Is there a generic version of Array.newInstance?

I noticed that in Java Array.newInstance() , Object returned, not T[] . This is understandable since this method was introduced before Java supports generic types.

However, it is unexpected that there is no equivalent general version of this. Java 7 Arrays.copyOf not the same - it copies the contents of the parameter, and does not create a new dummy array (with all null objects inside).

Since implementing this seems trivial, is there some reason not adding it to the JRE? or i just can't find it?

UPDATE

It seems that I will provide my own "trivial" implementation to prevent misunderstanding of the issue.

 class MyArrayUtil { //Generic version for classes @SuppressWarnings("unchecked") public static <T> T[] newArrayOf(T[] t, int len){ return (T[]) Array.newInstance(t.getClass().getComponentType(), len); } //For array of int public static int[] newArrayOf(int[] t, int len){ return new int[len]; } //For other primitive types... } 

I am not sending this code as an answer because it is not the answer to the question. The question is about the reason and / or existing code, and not how to implement it.

UPDATE

I updated the code to make it look like Arrays.copyOf , and the advantage is that the programmer can simply change the type of the parameter to set up the code for another type. I also ruled out using Array.newInstance for primitive types.

+8
java arrays generics
source share
3 answers

Guava provides just such a feature . This is not the first time that Guava (or Apache Commons) has provided a common helper that the JDK does not have for any reason.

You may know this, but there is some background for googler that will come across this in the future: the reason the signature cannot be shared is that the Array.newInstance method returned Object in Java 1.4 , so for backward compatibility the original version of the method should also return Object . If it was changed as:

 <T> T[] newInstance(Class<T> componentType, int length) 

... then the return type will be Object[] , not Object . This will violate the compatibility that Java developers have always tried not to do.

Arrays.copyOf methods appeared only with Java 1.6, and therefore there was no need to worry about backward compatibility.

+7
source share

No, you need to pass a specific type as a parameter anyway. As you mentioned, Arrays.copyOf shows this in action.

 Class<T> type = determinteTheTypeSomehow(); return (T[]) Array.newInstance(type, length); 
+4
source share

The main reason Array.newInstance() cannot be declared as <T> T[] newInstance(Class<T> class, int size) is because it can be used to create arrays of primitives. For example, if you pass an int.class that is of type Class<Integer> , then from the declaration you expect it to return an Integer[] object. However, the function actually returns an int[] object, which is not a subtype of Integer[] , so it has the wrong type.

Of course, they can add an additional method, for example newReferenceArrayInstance() , which prohibits primitive types (for example, throws an exception when passing a primitive type) and thus it is safe to declare return T[] . However, this is similar to adding a completely redundant method solely to eliminate uncontrolled casts.

+3
source share

All Articles