The generated arrays can be created using reflection (although unsafe selection is required), you just need to pass the class as a parameter (provided that the following class is inside the class that defines a parameter of type <T>
):
@SuppressWarnings("unchecked") public T[] createArray(Class<T> klass, int size) { return (T[]) Array.newInstance(klass, size); }
For example, in your case:
HashTable<Integer> t = new HashTable<Integer>(); Integer[] intArray = t.createArray(Integer.class, 4); intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; intArray[3] = 4; System.out.println(Arrays.toString(intArray)); > [1, 2, 3, 4]
source share