0

ArrayList<ArrayList> bigList = new ArrayList<ArrayList>();
ArrayList liste = new ArrayList();

for(i=0 ; i<10 ; i++){
liste.add(0,i);
bigList.add(0, liste);
liste.clear( );
}

What I want to get is something like [ [9] [8]...[0] ]. Instead of that I'm getting [ [ ] [ ]...[ ] ], an ArrayList of empty ArrayLists...
liste seems to be a "pointer" on a list so when I clear it, I'm clearing all the sub ArrayLists..., isn't?

How to fix the loop to get [ [9] [8]...[0] ]?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Spartan552
0

Correct, you want to set liste = new ArrayList(). Until you set the liste reference to point to a new ArrayList, you'll be operating on the same one as before.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.