I have the following code:
我有以下代码:
Integer[] lastExchange = new Integer[nColors];
Integer[] newExchange = new Integer[nColors];
while (true) {
...
for (int i=0; i<nColors; i++) {
lastExchange[i] = newExchange[i];
}
...
exchanges.add(lastExchange);
output.log.fine("Exchange:" + lastExchange[0] + "," + lastExchange[1]);
}
for (Integer[] exchange : exchanges) {
output.log.fine("Exchange:" + exchange[0] + "," + exchange[1]);
}
I have two outputs (one in the while loop another one in the for loop). The first output shows me that I do add different arrays to the list. While when i do a double check in the second loop I see that all elements of the exchange
list are the same (they are equal to the first element of the list).
我有两个输出(一个在while循环中,另一个在for循环中)。第一个输出显示我确实在列表中添加了不同的数组。当我在第二个循环中进行双重检查时,我看到交换列表中的所有元素都是相同的(它们等于列表的第一个元素)。
Does anybody know what I am doing wrong here?
有人知道我做错了什么吗?
2 个解决方案
#1
5
As unwind's answer states, you're adding a reference to the same array in every iteration of the loop. You need to create a new array each time:
作为unwind的回答状态,您将在循环的每次迭代中添加对相同数组的引用。您需要每次创建一个新的数组:
// It's not clear where newExchange is actually populated
Integer[] newExchange = new Integer[nColors];
while (true) {
Integer[] lastExchange = new Integer[nColors];
...
for (int i=0; i<nColors; i++) {
lastExchange[i] = newExchange[i];
}
...
exchanges.add(lastExchange);
output.log.fine("Exchange:" + lastExchange[0] + "," + lastExchange[1]);
}
Alternatively, if you're just cloning the array:
或者,如果你只是在克隆数组:
Integer[] newExchange = new Integer[nColors];
while (true) {
Integer[] lastExchange = newExchange.clone();
...
exchanges.add(lastExchange);
output.log.fine("Exchange:" + lastExchange[0] + "," + lastExchange[1]);
}
#2
2
What is the type of lastExchange
? If it's a reference to an object, the the problem is probably exactly that; you just add the same reference to a mutable object, which is then modified and added again.
什么是lastExchange?如果它是一个对象的引用,问题可能就是这个;您只需将相同的引用添加到一个可变对象,然后再修改和添加该对象。
Since the first loop prints the object before it (presumably) is modified, it prints the proper (different) values.
因为第一个循环在它(假定)被修改之前打印对象,所以它输出了正确的(不同的)值。
#1
5
As unwind's answer states, you're adding a reference to the same array in every iteration of the loop. You need to create a new array each time:
作为unwind的回答状态,您将在循环的每次迭代中添加对相同数组的引用。您需要每次创建一个新的数组:
// It's not clear where newExchange is actually populated
Integer[] newExchange = new Integer[nColors];
while (true) {
Integer[] lastExchange = new Integer[nColors];
...
for (int i=0; i<nColors; i++) {
lastExchange[i] = newExchange[i];
}
...
exchanges.add(lastExchange);
output.log.fine("Exchange:" + lastExchange[0] + "," + lastExchange[1]);
}
Alternatively, if you're just cloning the array:
或者,如果你只是在克隆数组:
Integer[] newExchange = new Integer[nColors];
while (true) {
Integer[] lastExchange = newExchange.clone();
...
exchanges.add(lastExchange);
output.log.fine("Exchange:" + lastExchange[0] + "," + lastExchange[1]);
}
#2
2
What is the type of lastExchange
? If it's a reference to an object, the the problem is probably exactly that; you just add the same reference to a mutable object, which is then modified and added again.
什么是lastExchange?如果它是一个对象的引用,问题可能就是这个;您只需将相同的引用添加到一个可变对象,然后再修改和添加该对象。
Since the first loop prints the object before it (presumably) is modified, it prints the proper (different) values.
因为第一个循环在它(假定)被修改之前打印对象,所以它输出了正确的(不同的)值。