How to add arrays to ArrayList?

前端 未结 3 1940
天涯浪人
天涯浪人 2021-01-24 16:11

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 w

相关标签:
3条回答
  • 2021-01-24 16:27

    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.

    0 讨论(0)
  • 2021-01-24 16:29

    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);
    
    }
    
    0 讨论(0)
  • 2021-01-24 16:35

    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]
    
    0 讨论(0)
提交回复
热议问题