How to add arrays to ArrayList?

霸气de小男生 提交于 2019-12-02 10:57:54

问题


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 

回答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);

}



回答2:


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.




回答3:


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]


来源:https://stackoverflow.com/questions/32040933/how-to-add-arrays-to-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!