aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About creating an array of ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About creating an array of ArrayList" Watch "About creating an array of ArrayList" New topic
Author

About creating an array of ArrayList

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, I was playing with this code:


But I got a warning here because ArrayList is a raw type.

But if I do this:

I got a compilation error because I am not allowed to create a generic array.

How can I eliminate the warning?

I hope this can be made a good exam reference.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Interesting question. I got this while searching.
Daniel Renner
Greenhorn

Joined: Mar 29, 2012
Posts: 10
Why not use 2 Lists? Or do you need the array? Or is it maybe good to use a map instead of this array+List?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3394
    
    9
Indeed. As Daniel suggested you could use
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Stephan,
List<List<String>> matrix = new ArrayList<>();

On the right hand side, can we put this List<String>, like this List<List<String>> matrix = new ArrayList<List<String>>();?


The reason why I raise this question because this can make a good exam question.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3394
    
    9
Yes you could do that, but as of Java 7 it's unnecessary. The compiler can determine what the type arguments of the ArrayList should be from the variable you're trying to assign it to. All you have to do is use the diamond operator <>.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About creating an array of ArrayList
 
Similar Threads
generics in arrays
ArrayList
Q about generic compiler warnings
generics doubt
Generic Array Creation Error Message