Due to the implementation of Java Generics you can't have code like this. How can I implement this while maintaining type safety?
public class GenSet<E> {
private E a[];
public GenSet()
{
a = new E[INITIAL_ARRAY_LENGTH];
}
}
I saw a solution on the java forums that goes like this:
import java.lang.reflect.Array;
class Stack<T> {
public Stack(Class<T> clazz,int capacity) {
array=(T[])Array.newInstance(clazz,capacity);
}
private final T[] array;
}
But I really don't get what's going on. Can anyone help? Thanks in advance.