Wie füge ich Arrays zu ArrayList hinzu?

Ich habe ein int [3] [3] -Array und es enthält nur 0 oder 1 Werte. Wenn der Wert 1 ist, möchte ich die Koordinaten dieses Werts in der ArrayList als int [2] -Array hinzufügen, aber ich nicht weiß warum es immer die letzten 1-wertigen Koordinaten hinzufügt, was ist das Problem?

public static void main(String[] args) {

    Random random = new Random();
    int[] coordinates = new int[2];
    ArrayList<int[]> arrayList = new ArrayList<>();
    int[][] board = new int[3][3];

    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            board[i][j] = random.nextInt(2);
        }
    }

    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            System.out.print(board[i][j] + " ");
            if (board[i][j] == 1){
                coordinates[0] = i;
                coordinates[1] = j;
                arrayList.add(coordinates);

            }
        }
        System.out.println();
    }

    System.out.println("coordinates of cells that contain 1 value");

    for (int[] coordianate : arrayList) {
        for (int i = 0; i < coordianate.length; i++) {
            System.out.print(coordianate[i] + " ");
        }
        System.out.println();
    }
}

}

Ausgabe

1 0 1 
1 1 0 
1 1 0 
coordinates of cells that contain 1 value
2 1 
2 1 
2 1 
2 1 
2 1 
2 1 

Antworten auf die Frage(6)

Ihre Antwort auf die Frage