Java 创建数组的数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18277657/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:58:31  来源:igfitidea点击:

Creating arraylist of arrays

javaarraysstringarraylist

提问by kjm

I'm trying to create an array list of string arrays. When I am done, I want the array list to look like this:

我正在尝试创建一个字符串数组的数组列表。完成后,我希望数组列表如下所示:

[0,0], [0,1], [1,0], [1,1,]

I have tried to define an array and then add it to the array list. Then re-define an array and add it again. But the array list only seems to contains the last entry. Take a look:

我试图定义一个数组,然后将其添加到数组列表中。然后重新定义一个数组并再次添加它。但是数组列表似乎只包含最后一个条目。看一看:

String[] t2 = new String[2]; 

ArrayList<String[]> list2 = new ArrayList<String[]>();

t2[0]="0";
t2[1]="0";
list2.add(t2);
t2[0]="0";
t2[1]="1";
list2.add(t2);
t2[0]="1";
t2[1]="0";
list2.add(t2);
t2[0]="1";
t2[1]="1";
list2.add(t2);

for (String[] tt : list2) 
{
System.out.print("[");
for (String s : tt)
System.out.print(s+" ");
System.out.print("]");
}

The output is:

输出是:

[1,1] [1,1] [1,1] [1,1]

[1,1] [1,1] [1,1] [1,1]

Any idea on how to add each array to my array list?`

关于如何将每个数组添加到我的数组列表中的任何想法?`

采纳答案by ktm5124

The problem is that you are adding the same object to each index of your ArrayList. Every time you modify it, you are modifying the same object. To solve the problem, you have to pass references to different objects.

问题是您要向 ArrayList 的每个索引添加相同的对象。每次修改它时,都是在修改同一个对象。为了解决这个问题,你必须传递对不同对象的引用。

String[] t2 = new String[2]; 

ArrayList<String[]> list2 = new ArrayList<String[]>();

t2[0]="0";
t2[1]="0";
list2.add(t2); 

t2 = new String[2]; // create a new array
t2[0]="0";
t2[1]="1";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="0";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="1";
list2.add(t2);

回答by Ted Hopp

You are adding the same array t2over and over. You need to add distinct arrays.

t2一遍又一遍地添加相同的数组。您需要添加不同的数组。

ArrayList<String[]> list2 = new ArrayList<String[]>();

list2.add(new String[] {"0", "0"});
list2.add(new String[] {"0", "1"});
list2.add(new String[] {"1", "0"});
list2.add(new String[] {"1", "1"});

As an aside: you can use Arrays.toString(tt)inside the loop to format each String[]the way you are doing.

顺便说一句:您可以Arrays.toString(tt)在循环内部使用String[]您正在做的每一种方式来格式化。

回答by Andy Thomas

You are only creating one array, and putting its reference repeatedly into the ArrayList.

您只创建一个数组,并将其引用重复放入 ArrayList。

Instead, you can create a new array for each element in the List:

相反,您可以为 List 中的每个元素创建一个新数组:

 ArrayList<String[]> list2 = new ArrayList<String[]>();

 String[] t2; 

 t2 = new String[2]; 
 t2[0]="0";
 t2[1]="0";
 list2.add(t2);

 t2 = new String[2]; 
 t2[0]="0";
 t2[1]="1";
 list2.add(t2);

 t2 = new String[2]; 
 t2[0]="1";
 t2[1]="0";

 t2 = new String[2]; 
 list2.add(t2);
 t2[0]="1";
 t2[1]="1";
 list2.add(t2);

回答by Tbonechk27

This is because you're adding t2 to all of the entries. Each time you overwrite t2 you're changing all the values. This is because t2 isn't being passed by value, it is being passed by reference and saved by reference.

这是因为您将 t2 添加到所有条目。每次覆盖 t2 时,您都会更改所有值。这是因为 t2 不是通过值传递,而是通过引用传递并通过引用保存。

回答by Bahram

You have just one item (t2) and each time you add it to the ArrayList . actually the reference of t2 is going to be saved in ArrayList so when you change the t2 value all of references in the ArrayList take effect.

您只有一项 (t2),每次将其添加到 ArrayList 时。实际上,t2 的引用将保存在 ArrayList 中,因此当您更改 t2 值时,ArrayList 中的所有引用都会生效。