开发者

Converting array of Objects to array of custom Types

开发者 https://www.devze.com 2023-03-14 18:01 出处:网络
I have a method to build the array for the required type. It works for the primitive types. But when I have arra开发者_JAVA百科y of custom objects it doesn\'t work. So I have tweaked it. But still it

I have a method to build the array for the required type. It works for the primitive types. But when I have arra开发者_JAVA百科y of custom objects it doesn't work. So I have tweaked it. But still it fails. Code is like this :

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

In this castToRequiredType : casts the CustomObject to the CustomType where CustomType is a class. And array to be build is of type CustomType. I am stuck at dynamically building the array of CustomType.

Any help in this direction is welcome. Thanks in advance.


If you have an array of Object, you can use Arrays.copyOf to convert it to a different type:

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


Thanks I have solved it using Axis's Array Util For the same

0

精彩评论

暂无评论...
验证码 换一张
取 消