File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes List[List[T]] vs. ArrayList[ArrayList[T]] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List[List[T]] vs. ArrayList[ArrayList[T]]" Watch "List[List[T]] vs. ArrayList[ArrayList[T]]" New topic
Author

List[List[T]] vs. ArrayList[ArrayList[T]]

Ed Beaty
Greenhorn

Joined: Apr 27, 2005
Posts: 6
Hi! I wrote a method that would iterate through a List of Lists of Integers and sum them all together. I ended up with:

public static int sum(List<List<Integer>> listOfLists){

int result = 0;
for (List<Integer> sublist: listOfLists)
for (Integer i: sublist)
result += i;
return result;
}

The problem is, when I try passing an ArrayList<ArrayList<Integer>> to the method, the program fails to compile with the error:

"The method sum(List<List<Integer>> in the type Test is not applicable for the arguments (ArrayList<ArrayList<Integer>> "

Worse, when I try to cast ArrayList<ArrayList<Integer>> to List<List<Integer>>, I get a "Cannot cast from ... to ..." error.

I'm sure I'm missing something here. Does anyone have any suggestions? I had hoped to eventually make a method to iterate through ANY Iterable<Iterable<Integer>> and sum them all, but my attempts fail with the same errors I described above.

Thanks for any help!
-Ed Beaty
Ed Beaty
Greenhorn

Joined: Apr 27, 2005
Posts: 6
D'oh! This seems to work nicely:

public static int sum(List< ? extends List<Integer>> listOfLists){

int result = 0;
for (List<Integer> sublist: listOfLists)
for (Integer i: sublist)
result += i;
return result;
}

Still trying to get the hang of generics...

Thanks,
Ed
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Since you're using Iterators, why limit it to Lists?
Ed Beaty
Greenhorn

Joined: Apr 27, 2005
Posts: 6
Or go one step further and have it work on any Iterable class:



In the process of trying to get this to work, I learned about the joy of erasure! I wanted to make an class that could iterate through all the elements in an Iterable of Iterable OR an Iterable of Iterator. This way, you could either combine multiple Lists into a single List or combine multiple Iterators into a List, and then iterate through all their elements (assuming the Lists and Iterators were all of the same type.) I tried using the constructots:

MergedIterator(Iterable<? extends Iterator<E>> manyIterators) or
MergedIterator(Iterable<? extends Iterable<E>> manyIterable)

but unfortunately, they both get erased to

MergedIterator(Iterable)

at compile-time, causing a duplicate method name error. Sigh.

-Ed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List[List[T]] vs. ArrayList[ArrayList[T]]
 
Similar Threads
New ArrayList Problem
Generics
Simple J2SE 5.0 Tiger Notes
Executors in Threads
Simple ArrayList conundrum?