generic arrays in List<T>.toArray(T[])
I have a list of MyClass, and I want to turn it into an array. I try:
myObjectList.toArray(new MyClass<String>[myObjectList.size()]);
But Exclipse 3.1M2 complains that I "Cannot create a generic array of MyClass<String>." I change to:
myObjectList.toArray(new MyClass[myObjectList.size()]);
Eclipse has no problems with this, but I don't think this matches the List<T> method signature, which is:
<T> T[] toArray(T[] a);
Which is correctme or Eclipse, and why?
Garret
I think you might need to read up on generics first. <String> would go after your collection class which i assume MyClass is not a list.
List<Integer> myIntList = new LinkedList<Integer>();
myIntList.add(new Integer(0));
Integer x = myIntList.iterator().next();
>I have a list of MyClass, and I want to turn it into an arrayNo you don't. You are creating an Array of MyClass in your example....
> I think you might need to read up on generics first.
Welcome to the forum, indeed...
> <String> would go after your collection class which i
Rightthat's the whole point of the questionwhy doesn't Eclipse allow the generic designation in the single argument to method <T> T[] List<T>.toArray(T[] a)?
> assume MyClass is not a list.
Congratulationsyou're correct. That's why I said, "I have a list of MyClass," instead of "I have MyClass, which is a list."
> You are creating an Array of MyClass in your example....
Brilliant! That's because <T> T[] List<T>.toArray(T[] a) takes an argument that is an array of MyClass. But I want an array of MyClass<String>, not just MyClasshence the question.
Here, let me spell it out:
final List<MyClass><String>> myObjectList=new ArrayList<MyClass><String>>();
myObjectList.add(new MyClass<String>("first item"));
myObjectList.add(new MyClass<String>("second item"));
Now read my message again, from the top. Continue reading until you reach the sentence, "I think you might need to read up on generics first."
Garret
Nice condescending manner you have there... Let's see if I can emulate it:
Perhaps if you had bothered to post this in the Generics forum, people might think you actually knew what you were doing.
Regarding your question, I've noted that Eclipse 3.1M2 is not yet entirely up to speed on all new features in Java 1.5, so I guess it might be at fault here.
BTW, what does the Java compiler say when you run it from the command line?
> Nice condescending manner you have there... Let's see
> if I can emulate it:
Goodness! I had thought I was being brushed off with the "I think you might need to read up on generics first" commentI was a big miffed that someone had made pot shots without reading what I actually wrote.
My sincere apologies for propogating condescension.
> Perhaps if you had bothered to post this in the
> Generics forum, people might think you actually knew
> what you were doing.
Ah, there it is, under "Special Topics." Thanks for pointing that out.
Garret
...and my apologies to bjon045 for being too reactionary.Garret
This is what I meant by my original comment.
>myObjectList.toArray(new MyClass<String>[myObjectList.size()]);
You are using an array not a list. Where did you get the idea that you can use an array with generics. Generics is mostly used with collections.
Read section 7.3 on the tutorial to see how to do it properly. http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Not only was I a bit overreactionary in my first response, it turns out there were a few things I could learn about generics. bjon045, thanks for the link to the great generics tutorial.
I wasn't familiar with method generics (I was wondering what the <T> was doing in the method signature). On the other hand, the List<T> source code uses T throughout, which didn't help my confusion, and didn't follow the suggestion in the tutorial: "If a generic method appears inside a generic class, it's a good idea to avoid using the same names for the type parameters of the method and class, to avoid confusion." (Section 5 page 10 of the tutorial)
Back to the core issue, here, it turns out that Eclipse was right: one cannot create an array of a generic type! (See Section 7.3 page 15 of the tutorial.) bjon045 asks why I thought that this was possible; there are several reasons. First, I had thought that the T in List<T> was the same as the T in the <T> toArray(T[]), and as my List<T> was of type List<MyClass><String>>, the method signature seemed to indicate a return type of the same type. That was my mistake.
But there are other reasons. If I can declare a standalone final MyClass<String> var=new MyClass<String>(), why can't i declare an array of them? This simply makes no sense to me.
Further, it's perfectly valid to have a method with a generic array as a parameter, such as verify(MyClass<String>[] myClasses). Now, if I can receive an array of generics in a method, I must have been able to create them at some pointthat reasoning seems perfectly logical to me. Java 5.0 generics apparently doesn't allow creating the array in a type-safe manner, though. Instead, I have to pass new MyClass[10] to the method, which means I lose the type-safety generics was supposed to bring.
In fact, the tutorial (Section 7.3 page 15) says that, "We've had type-safety as a primary design goal of generics. In particular, the language is designed to guarantee that if your entire application has been compiled without unchecked warnings using javac -source 1.5, it is type safe." (emphasis in original) Unfortunately, there's no way to use arrays of generics without unchecked warnings. Even if I cast my array of raw types to an array of generics when I pass it to a method that expects an array of generics, the cast is flagged as an unchecked cast.
I can, however, create a method with varargs (which we all know are implemented as arrays) such as verify(MyClass<String>... myClasses), and then inside the method assign the varargs to an array of MyClass<String>. In fact, it just occurs to me that I could create the following utility method:
public static <T> T[] createArray(T... items)
{
return items;
}
Won't this create an array of generics for me, with no unchecked warnings? (The problem is that with varargs I have to know at compile time how many items will be in the array when I call the method.)
This all doesn't make sense to me. What's the reasoning behind disallowing the creation of arrays of generics?
I'm going to move this thread over to the generics forum, so it would probably be better for any comments to be there.
Garret
My dear fellow , eclipse 3.1 does'nt support "j2se 5.0" tht's y its saying generic is not supported
My dear fellow , eclipse 3.1 does'nt support "j2se 5.0" tht's y its saying generic is not supported
> My dear fellow , eclipse 3.1 does'nt support "j2se
> 5.0"
> tht's y its saying generic is not supported
Au contraire!
1. As of Eclipse 3.1M2, "The Eclipse Java compiler now handles generics, enhanced for loops, and hexadecimal floating point literals (at this point we're passing 96.8% of the JCK1.5 compliance tests)." (See the "New and Noteworthy" for Eclipse 3.1M2.)
2. When Eclipse complains that it "Cannot create a generic array of MyClass<String>," it is actually saying that Java 5.0 does not allow creation of arrays of generic objects. This has no connotations regarding the Eclipse support of generics in general. In fact, it turns out that Eclipse is 100% correctJava 5.0 does not support the creation of arrays of generics, as I explain above and as I continue in the generics forum at:
http://forum.java.sun.com/thread.jsp?forum=316&thread=530823
Best,
Garret