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

Java 将变量添加到Arraylist,它将替换以前的所有元素,而变量不是静态的,java,arraylist,Java,Arraylist,我的问题与此类似。虽然我的变量不是静态的。但每次我添加一个值,其他值都将是该值 重要代码: 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 = ima

我的问题与此类似。虽然我的变量不是静态的。但每次我添加一个值,其他值都将是该值

重要代码:

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<int[]> listCONTOUR = new ArrayList<int[]>();
List<int[]> listDIRECTION = new ArrayList<int[]>();

// 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));
    }
}
int计数器=1;
//设置图像阈值以获得二值图像
图像。阈值(44);
image.showImage();
int[]directionFIRST=新的int[2];
//获取边界上的第一个白色像素
int[]pixelFIRST=image.getFirstBoundaryPixel();
updatePicture(pixelFIRST[0],pixelFIRST[1]);
directionFIRST=getInitialDirection(图像,像素优先);
//为输出创建一个数组。它将保持的(x,y)坐标
//待轮廓跟踪区域边界周围的每个像素。这个
//也会为链码保存方向。
List listCONTOUR=新建ArrayList();
List listDIRECTION=新建ArrayList();
//创建一个变量,用于告知算法何时停止:
布尔stopCondition=false;
int[][]ROTmatrix90=新的int[][{{0,1},{-1,0};
int[][]ROTmatrix180=新int[][{{-1,0},{0,-1};
int[]tempPIX=像素优先;
int[]tempDIR=directionFIRST;
while(!stopCondition){
//采取与当前方向相反的方向
tempDIR=乘法(rotmarix180,tempDIR);
tempPIX[0]=tempPIX[0]+tempDIR[0];
tempPIX[1]=tempPIX[1]+tempDIR[1];
if(image.get(tempPIX[0],tempPIX[1])==1){
添加(tempPIX);
添加(tempDIR);
}否则{
tempDIR=乘法(ROTmatrix90,tempDIR);
tempPIX[0]=tempPIX[0]+tempDIR[0];
tempPIX[1]=tempPIX[1]+tempDIR[1];
if(image.get(tempPIX[0],tempPIX[1])==1){
添加(tempPIX);
添加(tempDIR);
}否则{
tempDIR=乘法(ROTmatrix90,tempDIR);
tempPIX[0]=tempPIX[0]+tempDIR[0];
tempPIX[1]=tempPIX[1]+tempDIR[1];
if(image.get(tempPIX[0],tempPIX[1])==1){
添加(tempPIX);
添加(tempDIR);
}否则{
tempDIR=乘法(ROTmatrix90,tempDIR);
tempPIX[0]=tempPIX[0]+tempDIR[0];
tempPIX[1]=tempPIX[1]+tempDIR[1];
if(image.get(tempPIX[0],tempPIX[1])==1){
添加(tempPIX);
添加(tempDIR);
}否则{
tempDIR=乘法(ROTmatrix90,tempDIR);
tempPIX[0]=tempPIX[0]+tempDIR[0];
tempPIX[1]=tempPIX[1]+tempDIR[1];
if(image.get(tempPIX[0],tempPIX[1])==1){
添加(tempPIX);
添加(tempDIR);
}
}
}
}
}
计数器++;
updatePicture(tempPIX[0],tempPIX[1]);
System.out.println(tempPIX[0]+“,”+tempPIX[1]);
if(tempPIX[0]==tempPIX[1]){
系统输出打印(“测试”);
}
如果((listCONTOUR.size()>2)和&(tempPIX[0]==listCONTOUR.get(1)[0])&(tempPIX[0]==listCONTOUR.get(1)[1])){
stopCondition=true;
remove(listCONTOUR.get(listCONTOUR.size()-1));
remove(listDIRECTION.get(listDIRECTION.size()-1));
}
}

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

tempPIX
是指内存中的
int[]
数组

每次更新数组并将其添加到列表中时,只需将同一引用一次又一次地添加到同一数组中

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

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<int[]> listOfValues = new ArrayList<int[]>();
        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);
    }

}

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

每次更新数组并将其添加到列表中时,只需将同一引用一次又一次地添加到同一数组中

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

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<int[]> listOfValues = new ArrayList<int[]>();
        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);
    }

}

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

int[] tempPIX = pixelFIRST;
因此,您已经在while循环之外创建了这个数组。在while循环中,您正在修改数组并将其添加到列表中。 由于数组对象不是不可变的,因此,在更改内容时不会创建新的数组对象,而更改也会反映在列表中

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

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

tempPIX = temp;
并将此
数组
添加到您的
列表

您还需要将新数组重新分配给旧数组,以反映其中的更改(如第4行)

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

int[] tempPIX = pixelFIRST;
因此,您已经在while循环之外创建了这个数组。在while循环中,您正在修改数组并将其添加到列表中。 由于数组对象不是不可变的,因此,在更改内容时不会创建新的数组对象,而更改也会反映在列表中

您可以做的是,在while循环中创建一个新数组。一