How to create a generic array in Java?

Due to the implementation of Java generics, you can’t have code like this: public class GenSet<E> { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation } } How can I implement this while maintaining type safety? I saw a solution on the Java forums that goes like this: … Read more

How to create a generic array in Java?

I have to ask a question in return: is your GenSet “checked” or “unchecked”? What does that mean? Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of type E. See Collections.checkedCollection.-> in that case, you should … Read more