1

我有一种方法可以为所需类型构建数组。它适用于原始类型。但是当我有一组自定义对象时,它就不起作用了。所以我已经对其进行了调整。但它仍然失败。代码是这样的:

    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();
 }

在 this castToRequiredType: 中,将其转换CustomObjectCustomTypeCustomType。并且要构建的数组是类型CustomType。我被困在动态构建CustomType.

欢迎在这个方向提供任何帮助。提前致谢。

4

2 回答 2

2

如果您有一个 数组Object,您可以使用Arrays.copyOf它来将其转换为不同的类型:

CustomType[] ca = Arrays
  .copyOf(array, array.length, CustomType[].class);

于 2011-06-23T11:37:08.453 回答
1

谢谢,我已经使用Axis 的 Array Util 解决了同样的问题

于 2011-06-23T13:24:42.573 回答