Answer by Bill Michell for How can I create a generic array in Java?
Java generics work by checking types at compile time and inserting appropriate casts, but erasing the types in the compiled files. This makes generic libraries usable by code which doesn't understand...
View ArticleAnswer by Varkhan for How can I 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...
View ArticleAnswer by Jeff Olson for How can I create a generic array in Java?
This is covered in Chapter 5 (Generics) of Effective Java, 2nd Edition, item 25...Prefer lists to arraysYour code will work, although it will generate an unchecked warning (which you could suppress...
View ArticleAnswer by Esko for How can I create a generic array in Java?
You could create an Object array and cast it to E everywhere. Yeah, it's not very clean way to do it but it should at least work.
View ArticleAnswer by Ola Bini for How can I create a generic array in Java?
The example is using Java reflection to create an array. Doing this is generally not recommended, since it isn't typesafe. Instead, what you should do is just use an internal List, and avoid the array...
View ArticleHow can I 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...
View Article