如何创建一个新的ANYTYPE []数组? [英] How do I create a new AnyType[] array?

查看:775
本文介绍了如何创建一个新的ANYTYPE []数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在这种情况下,最好的做法?我想同样的类型和长度作为原始的未初始化数组。

 公共静态< ANYTYPE延伸可比&LT ;?超级ANYTYPE>>无效someFunction(ANYTYPE []的someArray){    ANYTYPE [] = anotherArray(ANYTYPE [])新可比[someArray.length]
     ...要么...
    ANYTYPE [] = anotherArray(ANYTYPE [])新对象[someArray.length]    ...其他一些code ...
}

谢谢,
CB


解决方案

  ANYTYPE [] = anotherArray(ANYTYPE [])java.lang.reflect.Array.newInstance(
      。someArray.getClass()getComponentType(),someArray.length);

Which is the best practice in this situation? I would like an un-initialized array of the same type and length as the original.

public static <AnyType extends Comparable<? super AnyType>> void someFunction(AnyType[] someArray) {

    AnyType[] anotherArray = (AnyType[]) new Comparable[someArray.length];
     ...or...
    AnyType[] anotherArray = (AnyType[]) new Object[someArray.length];

    ...some other code...
}

Thanks, CB

解决方案

AnyType[] anotherArray = (AnyType[])java.lang.reflect.Array.newInstance(
      someArray.getClass().getComponentType(), someArray.length);

这篇关于如何创建一个新的ANYTYPE []数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆