[英]ArrayList values set to last value after exiting for loop
我有一个方法应该从一个数据集创建一个Name
对象列表,该数据集包含一个名称和11个整数,表示几十年来该名称的流行度。
数据示例:
Zelda 436 420 468 526 789 961 938 0 0 0 0
Zulma 0 0 0 0 0 0 837 0 0 0 0
目前正确读取数据集,当我从for循环中检查Name
对象时,一切都是正确的。
但是在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;
}
我如何从主方法调用:
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[]
但同样的事情发生了。
我做错了什么,我该怎么做才能解决错误?
问题是你的Name
类可能定义为
class Name {
String name;
int[] popsInt;
Name(String name, int[] popsInt) {
this.name = name;
this.popsInt = popsInt)
}
}
因此,您要在Name
实例中存储对int[]
引用。 但是传递给构造函数的数组只有一个,它是在解析所有数据的循环之外构造的:
int popsInt = new int[11];
for (each game) {
popsInt = parse data
list.add(new Name(name, popsInt));
}
所以你通过将引用始终传递给同一个数组来构造一个Name
,所以最后它们都指向同一组11个值。
您必须复制传递的数据或在每次调用时分配新数组,例如:
for (each game) {
int[] popsInt = new int[11];
popsInt = parse data;
list.add(new Name(name, popsInt));
}
现在每个Name
实例都有自己的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.