添加变量ArrayList和它替换所有previous元素,而变量不是静态的 [英] Add variables to Arraylist and it replaces all previous elements , while variables are not static

查看:101
本文介绍了添加变量ArrayList和它替换所有previous元素,而变量不是静态的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是类似于此<一个href=\"http://stackoverflow.com/questions/2447942/add-elements-to-arraylist-and-it-replaces-all-$p$pvious-elements-in-java\">Add元素ArrayList和它取代Java中所​​有 previous元素。虽然我的变量不是一成不变的。尽管如此,每次我加一个,另一个值将是该值。

My question is similar to this one Add elements to Arraylist and it replaces all previous elements in Java . Though my variables are not static. Still everytime I add one, the other values will be that value.

重要code:

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));
    }
}

当我检查listCONTOUR的值可以经过说循环的5次运行中,所有的值都是相同的,这是不可能的。我搜索一个解决方案,但所有方案都朝着一个事实,即该变量是静态的指向。虽然在我的情况并非如此。它仅仅是其一个函数发起和1函数内使用的一个简单的局部变量

When I check the values of listCONTOUR after lets say 5 runs of the loop, all the values are the same, which is not possible. I searched for a solution, but all solutions are pointing towards the fact that the variable is static. While in my case it isn't. It is just a simple local variable which is initiated in a function and used within 1 function.

推荐答案

tempPIX 是引用一个 INT [] 阵列中存储器

tempPIX is reference to an int[] array in memory.

每个更新的数组并把它添加到列表中的时候,你只是一遍又一遍地将相同的参考同一个数组。

Each time you update the array and add it to the list, you are simply adding the same reference to the same array over and over again.

有一个更好的解决办法是在每次循环中创建新阵列...

A better solution would be to create a new array on each loop...

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

从评论更新时间:

嗯,我能说的是,我不知道你在做什么...

Well, all I can say is, I don't know what you're doing...

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);
    }

}

它输出类似...

Which outputs something like...

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

这篇关于添加变量ArrayList和它替换所有previous元素,而变量不是静态的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆