I already read some of the answers following which I know 1 of the technique of creating a generic array is using reflection but what of I write the below generic method for creating an array? Is this potentially dangerous in any way?
public <E> E[] getArray(int size, Class<E> e) {
E[] arr = (E[])new Object[size];
return arr;
}
Objectnot an array ofE. Have a look at how to do it.Class<E> eyou're planning to use for ?Einto an element of the array, you'll get an exception.Class<E> eis unused in your current code. If you remove it, your method invocation would look like :Integer[] strArr = myStack.getArray(10).. What's still an unsolved piece of this puzzle is what ismyStackan instance of, does it imply any type bound to it?