简体   繁体   中英

How to add arrays to ArrayList?

I have an int[3][3] array and it contains only 0 or 1 values, if the value is 1 I want to add the coordinates of this value in the ArrayList as int[2] array, but I don't know why it always add the last 1-value coordinates, what's the 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();
    }
}

}

output:

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 

You need to create new coordinates array for each i , j pair you want to place in your list. For now you are placing same array multiple times which remembers last set pair.

In other words you need to

if (board[i][j] == 1) {
    coordinates = new int[2];//add this line
    coordinates[0] = i;
    coordinates[1] = j;
    arrayList.add(coordinates);

}

You need to create a new coordinate object every time you add it:

if (board[i][j] == 1) {
  int[] coordinate = new int[2];
  coordinate[0] = i;
  coordinate[1] = j;
  arrayList.add(coordinate);
}

or shorter:

if (board[i][j] == 1) {
    arrayList.add(new int[]{i, j} );
}

Otherwise you will add the same object multiple times and modify it each time so only the last coordinate remains.

If you make it a habit to use narrow scoped (temporary) variables, this typically comes naturally as you do not drag state around outside of loops.

If you are putting Points into an ArrayList, I suggest you create a Point object that takes coordinates. Please refer to the code below. Sorry for the lengthy reply.

import java.util.ArrayList;
import java.util.Arrays;

public class SomeClass {

    static class Point {

        int[] coordinates;

        public Point(int x, int y) {
            this.coordinates = new int[2];
            this.coordinates[0] = x;
            this.coordinates[1] = y;
        }

        public Point() {
            this(0,0);
        }

        public Point(int[] coordinates) {
            this.coordinates = coordinates;
        }
    }

    public static void main(String[] args) {
        SomeClass myClass = new SomeClass();
        Point a = new Point();
        Point b = new Point(5,5);
        Point c = new Point(new int[]{3,4});

        ArrayList<Point> arr = new ArrayList<Point>();

        // adding
        arr.add(a);
        arr.add(b);
        arr.add(c);

        // retrieve one object
        int index = 0;
        Point retrieved = arr.get(index);
        System.out.println("Retrieved coordinate: " + Arrays.toString(retrieved.coordinates));
        retrieved.coordinates[0] = 15;
        retrieved.coordinates[1] = 51;
        System.out.println("After change, retrieved coordinate: " + Arrays.toString(retrieved.coordinates));
        System.out.println("After change, accessing arraylist index: " + Arrays.toString(arr.get(index).coordinates));

        // we received a pointer to the array
        // changed values are automatically reflected in the ArrayList

    }
}

These are the values you will get...

Retrieved coordinate: [0, 0]
After change, retrieved coordinate: [15, 51]
After change, accessing arraylist index: [15, 51]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM