Two methods for creating shared arrays
I learned about two methods for creating shared arrays.
One
@SuppressWarnings("unchecked") static <T> T[] array1(final Class<T> elementType, final int size) { return (T[]) Array.newInstance(elementType, size); } And the other
static <T> T[] array2(final Class<T[]> arrayType, final int size) { return arrayType.cast(Array.newInstance(arrayType.getComponentType(), size)); } What's better? Are they the same (internally)? Is any case wrong?
Behind the scenes, both do the same, except that in option 1 you pass an object of class T , and in option 2 you pass in T [].
I prefer option 1 because it is shorter and easier to read. Again, this is the same as Array.newInstance with the addition of a throw, so I'm not sure if your method adds more value. Array.newInstance