将变量添加到Arraylist并替换所有先前的元素,而变量不是静态的

我的问题类似于这个向Arraylist添加元素,它取代了Java中所有以前的元素 。 虽然我的变量不是静态的。 仍然每次我添加一个,其他值将是该值。

重要代码:

int counter = 1; // Threshold the image to get a binary image image.threshold(44); image.showImage(); int[] directionFIRST = new int[2]; // Get the first white pixel on the boundary int[] pixelFIRST = image.getFirstBoundaryPixel(); image.updatePicture(pixelFIRST[0], pixelFIRST[1]); directionFIRST = getInitialDirection(image, pixelFIRST); //Create an array for the output. It will hold the (x,y) coordinates of //every pixel around the border of the region to be contour-traced. The //directions are also saved for the chain code. List listCONTOUR = new ArrayList(); List listDIRECTION = new ArrayList(); // Create a variable which will be used to tell the algorithm when to stop: boolean stopCondition = false; int[][] ROTmatrix90 = new int[][]{{0, 1}, {-1, 0}}; int[][] ROTmatrix180 = new int[][]{{-1, 0}, {0, -1}}; int[] tempPIX = pixelFIRST; int[] tempDIR = directionFIRST; while (!stopCondition) { //Take the direction opposit the current direction tempDIR = multiply(ROTmatrix180, tempDIR); tempPIX[0] = tempPIX[0] + tempDIR[0]; tempPIX[1] = tempPIX[1] + tempDIR[1]; if (image.get(tempPIX[0], tempPIX[1]) == 1) { listCONTOUR.add(tempPIX); listDIRECTION.add(tempDIR); } else { tempDIR = multiply(ROTmatrix90, tempDIR); tempPIX[0] = tempPIX[0] + tempDIR[0]; tempPIX[1] = tempPIX[1] + tempDIR[1]; if (image.get(tempPIX[0], tempPIX[1]) == 1) { listCONTOUR.add(tempPIX); listDIRECTION.add(tempDIR); } else { tempDIR = multiply(ROTmatrix90, tempDIR); tempPIX[0] = tempPIX[0] + tempDIR[0]; tempPIX[1] = tempPIX[1] + tempDIR[1]; if (image.get(tempPIX[0], tempPIX[1]) == 1) { listCONTOUR.add(tempPIX); listDIRECTION.add(tempDIR); } else { tempDIR = multiply(ROTmatrix90, tempDIR); tempPIX[0] = tempPIX[0] + tempDIR[0]; tempPIX[1] = tempPIX[1] + tempDIR[1]; if (image.get(tempPIX[0], tempPIX[1]) == 1) { listCONTOUR.add(tempPIX); listDIRECTION.add(tempDIR); } else { tempDIR = multiply(ROTmatrix90, tempDIR); tempPIX[0] = tempPIX[0] + tempDIR[0]; tempPIX[1] = tempPIX[1] + tempDIR[1]; if (image.get(tempPIX[0], tempPIX[1]) == 1) { listCONTOUR.add(tempPIX); listDIRECTION.add(tempDIR); } } } } } counter++; image.updatePicture(tempPIX[0], tempPIX[1]); System.out.println(tempPIX[0] + " , " + tempPIX[1]); if(tempPIX[0]== tempPIX[1]){ System.out.println("test"); } if ((listCONTOUR.size() > 2) && (tempPIX[0] == listCONTOUR.get(1)[0]) && (tempPIX[0] == listCONTOUR.get(1)[1])) { stopCondition = true; listCONTOUR.remove(listCONTOUR.get(listCONTOUR.size() - 1)); listDIRECTION.remove(listDIRECTION.get(listDIRECTION.size() - 1)); } } 

当我在循环5次运行之后检查listCONTOUR的值时,所有值都是相同的,这是不可能的。 我搜索了一个解决方案,但所有解决方案都指向变量是静态的这一事实。 虽然在我的情况下它不是。 它只是一个简单的局部变量,它在一个函数中启动并在一个函数中使用。

tempPIX引用内存中的int[]数组。

每次更新arrays并将其添加到列表中时,您只需一遍又一遍地向同一arrays添加相同的引用。

更好的解决方案是在每个循环上创建一个新数组……

 int[] tmpAry = new int[2]; tmpAry[0] = ntempPIX[0] + tempDIR[0]; tmpAry[1] = tempPIX[1] + tempDIR[1]; tempPIX = tmpAry; // Reassign the reference so the rest of the code doesn't need to be updated 

更新评论

好吧,我只能说,我不知道你在做什么……

 public class TestArrays { public static void main(String[] args) { List listOfValues = new ArrayList(); int[] outter = new int[] {1, 2, 3, 4}; listOfValues.add(outter); dump(outter); for (int index = 0; index < 5; index++) { int[] inner = new int[] { rand(), rand(), rand(), rand() }; outter = inner; dump(outter); listOfValues.add(outter); } int index = 0; for (int[] values : listOfValues) { System.out.print("[" + index + "] "); dump(values); index++; } } public static void dump(int[] values) { for (int value : values) { System.out.print(value + ", "); } System.out.println("\b\b"); // Cheeck...;) } public static int rand() { return (int)Math.round(Math.random() * 100); } } 

哪个输出像......

 1, 2, 3, 4 44, 35, 76, 9 44, 11, 17, 35 99, 24, 39, 23 20, 31, 9, 66 45, 50, 60, 27 [0] 1, 2, 3, 4 [1] 44, 35, 76, 9 [2] 44, 11, 17, 35 [3] 99, 24, 39, 23 [4] 20, 31, 9, 66 [5] 45, 50, 60, 27 

好的,问题是您正在更改相同的array object并将其引用推送到列表中。 因此,当数组对象发生更改时,它也会反映在指向它的所有引用中。

 int[] tempPIX = pixelFIRST; 

所以,你已经在while循环之外创建了这个数组。 在while循环中,您正在修改数组并添加到列表中。 由于数组对象不是immutable因此在更改内容时不会创建新的数组对象,而是更改也会反映在列表中。

你可以做的是,在while循环中创建一个新数组。 并复制该数组中的内容。

 int[] temp = new int[2]; temp[0] = tempPIX[0] + tempDIR[0]; temp[1] = tempPIX[1] + tempDIR[1]; tempPIX = temp; 

并将此array添加到您的List 。 您还需要将新数组重新分配给旧数组以反映其中的更改(如第4行所示)