Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new AnyType[] array?

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

like image 936
cb. Avatar asked Dec 04 '22 12:12

cb.


1 Answers

AnyType[] anotherArray = (AnyType[])java.lang.reflect.Array.newInstance(
      someArray.getClass().getComponentType(), someArray.length);
like image 129
Dimitris Andreou Avatar answered Dec 27 '22 15:12

Dimitris Andreou