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] ]?

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.