Jump to content
  • Advertisement
Sign in to follow this  
tufflax

[java] Array of ArrayListMyClass creation :S

This topic is 4384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I'm trying to create an array of ArrayLists. But I'm doing something wrong because I can't do it without getting warnings. ArrayList<MyClass>[] list; list = new ArrayList<MyClass>[100]; <- error: generic array creation list = new ArrayList[100]; <- warning: unchecked conversion for (...) list = new ArrayList<MyClass>; What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I'm still using 1.4 here so I can't check this out but my gut feeling is that you probably can't do this, purely from the compilers point of view. It probably barfs on the combination. I'd happily be proven wrong though. Just live with the warnings and do it without the generics ...

D.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm still using 1.4 here so I can't check this out but my gut feeling is that you probably can't do this, purely from the compilers point of view. It probably barfs on the combination. I'd happily be proven wrong though. Just live with the warnings and do it without the generics ...

D.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by RayNbow
Something I found about array generics:
Java Technology Forums - Clarification for Array Types and generics


well there you go. Generics were always a bit of a hack anyway. I was under the impression when I first started using them (exclusively 1.5 in work) that the RE actually 'knew' about them but its actually quite possible I discovered to compile 1.5 source containing generics to conform to 1.4 bytecode, so its literally just a compile time thing. They're useful for sanity checking while coding, and not having to write out all those casts, little else.

D.

Share this post


Link to post
Share on other sites
yeah, this is some funness right here:

ArrayList<String> arrStr = new ArrayList<String>();
ArrayList<Integer> arrInt = new ArrayList<Integer();
arrStr.add("Wot wot?!?");
arrInt.add(new Integer(13));

ArrayList arrObj = arrInt; //this is fine, no problem here
arrStr = (ArrayList<String>)arrObj; //so far, no complaints from compiler or runtime.

System.out.println(arrStr.get(1)); //RUNTIME KABOOM!!!


To me, a proper generics system should definitely not allow the 6th line, and probably shouldn't allow the 5th line, with all of this checking at COMPILE time, not runtime.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!