简体   繁体   中英

JAVA, list of lists

I have list and list of lists:

ArrayList<String> singleList = new ArrayList<String>();
ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();

I do not understand the behavior of these lists. I decided to show you a simple example:

listOfLists.clear();

singleList.clear();
singleList.add("A");
singleList.add("B");
singleList.add("C");
listOfLists.add(singleList);

singleList.clear();
singleList.add("D");
singleList.add("E");
singleList.add("F");
listOfLists.add(singleList);

singleList.clear();
singleList.add("G");
singleList.add("H");
singleList.add("I");
listOfLists.add(singleList);

for(int x = 0; x < listOfLists.size(); x++)
{
    for(int z = 0; z < singleList.size(); z++)
        {
        System.out.print(listOfLists.get(x).get(z));
        System.out.print(" ");
    }
    System.out.println("");
}

And the result I got was:

GHIGHIGHI

Instead:

ABCDEFGHI

Where is a problem with my thinking? What should I do to get result as above?

Objects are always passed as references in Java.

When you add singleList to listOfLists , you are in fact adding a reference to singleList . Since you've added it 3 times, you got the current value of singleList , repeated 3 times.

The "previous values" of singleList are stored nowhere, so ABC and DEF are lost.

You need to make a copy of your list, by using new ArrayList<String>(singleList) . Then, add this copy to listOfLists .

The problem is how object references work. Going step by step

singleList.clear();
singleList.add("A");
singleList.add("B");
singleList.add("C");
listOfLists.add(singleList);

//singleList -> A, B, C 
//listOfLists -> singleList

singleList.clear();
singleList.add("D");
singleList.add("E");
singleList.add("F");
listOfLists.add(singleList);

//singleList -> D, E, F
//listOfLists -> singleList, singleList

singleList.clear();
singleList.add("G");
singleList.add("H");
singleList.add("I");
listOfLists.add(singleList);

//singleList -> G, H, I
//listOfLists -> singleList, singleList, singleList

Now, you are printing listOfLists, wich contains 3 times singleList. But singleList contains now G, H, I

To get the desired result, you need to use different lists, one with A, B, C, other with D, E, F, and another one with G, H, I.

singleList1 -> A, B, C
singleList2 -> D, E, F
singleList3 -> F, G, H
listOfLists -> singleList1, singleList2, singleList3

Instead of having different variables for singleList, you can also do this when adding to listOfLists: listOfLists.add(new ArrayList(singleList));

this creates a copy of singlelist that has a different memory location unlike the one you did which refers to the same location.

To make a list of lists, you can easily use ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<ArrayList<Integer>>; to make a list of lists of integers, or replaces Integer with String or whatever you want. To access items, use listOfLists.get(0).get(0) for example, to get the first item of the first list inside of listOfLists . Not sure if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM