create a generic array in java

public <E> E[] getArray(Class<E> clazz, int size) { 
  @SuppressWarnings("unchecked")
  E[] arr = (E[]) Array.newInstance(clazz, size);
  return arr;
}

Reference:
http://stackoverflow.com/questions/18581002/how-to-create-a-generic-array
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html (Type Erasure)
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html (checkedCollection)
http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java

Create instance of generic type in java
http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java

Advertisements

About yingding wang
i am looking forward to innovation in IT and Robotics

Comments are closed.

%d bloggers like this: