【发布时间】:2011-06-23 11:33:46
【问题描述】:
我有一种方法可以为所需类型构建数组。它适用于原始类型。但是当我有自定义对象数组时它不起作用。所以我已经对其进行了调整。但它仍然失败。 代码是这样的:
private Object buildArray( String type, Object object) {
final Class<?> requiredType = loadClass(type);
final String typeName = type.substring(2).replace(";", "").trim();
Object[] array = ((Object[]) object);
ArrayList<Object> arrayList = new ArrayList<Object>(array.length);
for (Object customObj : array) {
arrayList.add(castToRequiredType(typeName, customObj));
}
return arrayList.toArray();
}
在这个castToRequiredType 中:将CustomObject 转换为CustomType,其中CustomType 是一个类。并且要构建的数组的类型是CustomType。我被困在动态构建CustomType 的数组中。
欢迎在这方面提供任何帮助。 提前致谢。
【问题讨论】:
-
这段代码的意图真的很难理解。您对此的总体目标是什么?
-
总体目标是调用采用 customObject[] 类型参数的 API。我必须使用 castToRequiredType 构建每个 CustomObject。此 CustomObject 可以是 MyCustoType、YourCustomType 等。
标签: java arrays list reflection toarray