在这种情况下,最佳做法是什么?我想要一个与原始数组具有相同类型和长度的未初始化数组。
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...
}
谢谢, 炭黑
最佳答案
AnyType[] anotherArray = (AnyType[])java.lang.reflect.Array.newInstance(
someArray.getClass().getComponentType(), someArray.length);
关于java - 如何创建新的 AnyType[] 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2664594/