File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes arrays of ArrayList confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays of ArrayList confusion" Watch "arrays of ArrayList confusion" New topic
Author

arrays of ArrayList confusion

Rajat Sarkar
Greenhorn

Joined: Sep 07, 2008
Posts: 18
Can we declare arrays of arrays of ArrayList?

i.e. ArrayList<String> myList=new ArrayList<String>[5]; // is this valid ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
No.

I can't remember why, but a search of this forum will reveal old posts about the same subject.
You cannot create an array of parameterised type. So List[] myLists = new ArrayList[3]; is permissible. But List<Foo>[] myFooLists = new ArrayList<Foo>[3]; is not permissible. It has something to do with erasure rather than reification in generics, but it is better explained in the older posts.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Rajat Sarkar:
Can we declare arrays of arrays of ArrayList?

i.e. ArrayList<String> myList=new ArrayList<String>[5]; // is this valid ??


Not like that, but yes, you can create arrays of Arraylists:



But I wouldn't advise to do it like that. The (rather ugly) casting will also cause the compiler to cough up warning(s) (not errors!). So I advise to use List<List<String>>, or something similar instead.
[ October 15, 2008: Message edited by: Piet Verdriet ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays of ArrayList confusion
 
Similar Threads
Generics and Array conversion contradication
Generics ArrayList
Java 5 - Array of ArrayList&lt;String&gt; ?
Mock question for ArrayList ( Drag and drop )
Collections and Generics