我有一个方法,应该创建一个列表Name
对象来自一个数据集,其中包含一个名称和11个整数,表示该名称在数十年中的流行度。数据退出for循环后ArrayList值设置为最后一个值
例子:
Zelda 436 420 468 526 789 961 938 0 0 0 0
Zulma 0 0 0 0 0 0 837 0 0 0 0
目前的数据集被正确读取,当我检查Name
对象从内部for循环的一切是正确的。
但在for
循环退出后,ArrayList
中的所有值都具有与最后一项相同的流行值,而name
值保持正确。
应该发生什么:
Zelda 436 420 468 526 789 961 938 0 0 0 0
Zelda [436, 420, 468, 526, 789, 961, 938, 0, 0, 0, 0]
实际发生的:
Zelda 436 420 468 526 789 961 938 0 0 0 0
Zelda [0, 0, 0, 0, 0, 0, 837, 0, 0, 0, 0]
数据和最后一个条目的输出:
Zulma 0 0 0 0 0 0 837 0 0 0 0
Zulma [0, 0, 0, 0, 0, 0, 837, 0, 0, 0, 0]
代码:
public static ArrayList<Name> createNameArray(String[] data) {
int nameLength;
String name;
String pops;
ArrayList<Name> names = new ArrayList<Name>();
int[] popsInts = new int[11];
for (int i = 0; i < data.length; i++) {
// Checking data array, no missing data here
System.out.println(data[i]); //returns correctly
nameLength = data[i].indexOf(' ');
name = data[i].substring(0, nameLength);
pops = data[i].substring(nameLength + 1);
for (int k = 0; k < 11; k++) {
popsInts[k] = Integer.parseInt(pops.split(" ")[k]);
}
names.add(new Name(name, popsInts));
// Checking if Name object added to names is correct which it always is
System.out.println(names.get(i)); //returns correctly
}
// If I print out values of the ArrayList here, everything is wrong
return names;
}
如何我从main方法调用:
ArrayList<Name> list = createNameArray(data);
// Printing out ArrayList, all values are wrong except last one
// Using a regular for loop produces same results
for(Name n : list) {
System.out.println(n);
}
我一直在使用一个Name[]
尝试,但同样的事情发生。
我在做什么错,我该怎么办才能修复错误?
在循环**中创建'int [] popsInts = new int [11];'**。现在,你有一个数组,你多次添加到你的'List'。 –
'Name'的实现是什么? –
@ElliottFrisch工作,但我不明白为什么它的工作。 popInts在每个循环都更改为新值,并且在将每个名称对象添加到打印正确值的ArrayList后打印每个名称对象。如何在循环外部创建int数组对循环退出后发生的情况产生影响,即使循环内部它完美工作?感谢通过这种方式的快速反应:)编辑:杰克在他们的答案解释。 –