JAVA,列表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19605812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA, list of lists
提问by Mariusz Jucha
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:
我得到的结果是:
G H I G H I G H I
高辉
Instead:
反而:
A B C D E F G H I
ABC DEF GHI
Where is a problem with my thinking? What should I do to get result as above?
我的思维哪里出了问题?我应该怎么做才能得到上述结果?
采纳答案by Guillaume Poussel
Objects are alwayspassed as references in Java.
在 Java 中,对象总是作为引用传递。
When you add singleList
to listOfLists
, you are in fact adding a referenceto singleList
. Since you've added it 3 times, you got the current value of singleList
, repeated 3 times.
当您添加singleList
到listOfLists
,你实际上添加的引用来singleList
。由于您已经添加了 3 次,因此您得到了 的当前值singleList
,重复了 3 次。
The "previous values"of singleList
are stored nowhere, so A B C
and D E F
are lost.
该“以前的值”的singleList
存储不通,所以A B C
和D E F
丢失。
You need to make a copy of your list, by using new ArrayList<String>(singleList)
. Then, add this copy to listOfLists
.
您需要使用new ArrayList<String>(singleList)
. 然后,将此副本添加到listOfLists
.
回答by Evans
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
现在,您正在打印 listOfLists,其中包含 3 个 singleList。但是 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
回答by user4857117
Instead of having different variables for singleList, you can also do this when adding to listOfLists: listOfLists.add(new ArrayList(singleList));
除了为 singleList 设置不同的变量之外,您还可以在添加到 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.
这将创建一个具有不同内存位置的 singlelist 副本,这与您所做的引用相同位置的内存位置不同。
回答by Personinous Apraham
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.
要制作列表列表,您可以轻松地使用ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<ArrayList<Integer>>;
制作整数列表列表,或者替换Integer
为String
或任何您想要的。要访问项目,请使用listOfLists.get(0).get(0)
例如,获取 中第一个列表的第一个项目listOfLists
。不确定这是否有帮助。