• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array of ArrayList<String>  RSS feed

 
Vinod James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wanted 5 ArrayList<String> references.
I declared the following in my program
ArrayList<String> []outerlist;
This declaration works fine.
when I try to instantiate like ArrayList<String>outerlist= new ArrayList<String>[5] it complains that I cannot create a Array of Generic Arrays.

so what I did is create ArrayList<ArrayList<String>> and then
ArrayList<ArrayList<String>>outerlist= new ArrayList<ArrayList<String>>() and then whenever needed I created inner lists and added them to the outer list.
I am still wondering how to create an array of Arraylists.

My understanding is that
ArrayList<String>outerlist= new ArrayList<String>[5]
should create 5 references to ArrayList<String>.
Can somebody please explain why this doesnt work.

I really need a very basic book on generics. I am finding the topic of generics too difficult


Vinod James
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod James wrote:I am still wondering how to create an array of Arraylists.

It's one of the basic problems with Java generics: you can't create generic arrays.

You can do it with a cast, viz:but to be honest, your solution is probably best.

BTW, even better is probably:
List<List<String>> outerlist= new ArrayList<List<String>>();
because then you can choose the type of your inner and outer Lists as you want.

HIH

Winston
 
Vinod James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply Winston.
I am a newbie to java hence my questions might be little too basic.
When I say Dog []d = new Dog[5];
d holds 5 references to dog objects and then the 5 dog objects must be instantiated and assigned

I was extending the same logic and expecting 5 references to ArrayList<String> when I do the following
ArrayList<String>outerlist= new ArrayList<String>[5]

Is this due to Type Erasure?
What happened behind the scene at the compiler?

Why in the Dog[] d case it is able to create references to Dog objects and why in the ArrayList<String>[] it is not able to create the referencs?

Thanks
Vinod James



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod James wrote:Is this due to Type Erasure?

Partly.

What happened behind the scene at the compiler?

Ooof. Dunno; and anything I might say would be pure speculation. What I can tell you is that as far as the compiler is concerned, the <String> in ArrayList<String> basically doesn't exist; hence the reason why you need to cast as I showed you above.

There's a fairly good discussion here if you're interested.

Plus, as I said, your solution is much better because, unless you're worried about saving a few nanoseconds (and believe me, you're NOT), Lists are almost always better than arrays.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

As Winston has hinted, you should make performence the last consideration when you design code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!