[英]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 GHIGHIGHI
Instead: 代替:
ABCDEFGHI 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. 对象始终作为Java中的引用传递。
When you add singleList
to listOfLists
, you are in fact adding a reference to singleList
. 将singleList
添加到listOfLists
,实际上是在添加对singleList
的引用 。 Since you've added it 3 times, you got the current value of singleList
, repeated 3 times. 由于你已经添加了3次,你得到了singleList
的当前值,重复了3次。
The "previous values" of singleList
are stored nowhere, so ABC
and DEF
are lost. singleList
的“先前值”无处存储,因此ABC
和DEF
丢失。
You need to make a copy of your list, by using new ArrayList<String>(singleList)
. 您需要使用new ArrayList<String>(singleList)
制作列表的副本。 Then, add this copy to listOfLists
. 然后,将此副本添加到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. 现在,您正在打印listOfLists,它包含3次singleList。 But singleList contains now G, H, I 但是singleList现在包含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. 要获得所需的结果,您需要使用不同的列表,一个是A,B,C,另一个是D,E,F,另一个是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)); 您可以在添加到listOfLists时执行此操作,而不是为singleList提供不同的变量: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>>;
要创建列表列表,可以轻松使用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. 制作整数列表的列表,或用String
或任何你想要的东西替换Integer
。 To access items, use listOfLists.get(0).get(0)
for example, to get the first item of the first list inside of listOfLists
. 要访问项目,请使用listOfLists.get(0).get(0)
,例如,获取listOfLists
第一个列表的第一项。 Not sure if this helps. 不确定这是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.