0

私の質問はこれに似ています。Arraylistに要素を追加すると、Javaの以前のすべての要素が置き換えられます。私の変数は静的ではありませんが。それでも、1つ追加するたびに、他の値はその値になります。

重要なコード:

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

ループを5回実行した後、listCONTOURの値を確認すると、すべての値が同じであるため、不可能です。解決策を探しましたが、すべての解決策は変数が静的であるという事実を示しています。私の場合はそうではありませんが。これは、関数で開始され、1つの関数内で使用される単純なローカル変数です。

4

2 に答える 2

2

tempPIXint[]メモリ内の配列への参照です。

配列を更新してリストに追加するたびに、同じ配列に同じ参照を何度も追加するだけです。

より良い解決策は、各ループに新しい配列を作成することです...

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

}

これは次のようなものを出力します...

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
于 2012-10-17T08:12:29.927 に答える
1

問題は、同じものを変更して、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行目のように)

于 2012-10-17T08:15:52.623 に答える