【问题标题】:How to add arrays to ArrayList?如何将数组添加到 ArrayList?
【发布时间】:2015-08-16 23:28:26
【问题描述】:

我有一个 int[3][3] 数组,它只包含 0 或 1 个值, 如果值为1我想在ArrayList中添加这个值的坐标为int[2]数组,但是不知道为什么总是添加最后一个1值的坐标,有什么问题?

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

}

输出:

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 

【问题讨论】:

    标签: java arrays arraylist add


    【解决方案1】:

    您需要为要放入列表中的每个 i,j 对创建新的 coordinates 数组。现在你要多次放置 同一个数组,它会记住最后一组对。

    换句话说,你需要

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

    【讨论】:

      【解决方案2】:

      每次添加时都需要创建一个新的坐标对象:

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

      或更短:

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

      否则,您将多次添加同一个对象并每次修改它,因此只保留最后一个坐标。

      如果您养成使用窄范围(临时)变量的习惯,这通常会很自然,因为您不会在循环之外拖动状态。

      【讨论】:

        【解决方案3】:

        如果你将 Points 放入 ArrayList,我建议你创建一个 Point 对象来获取坐标。请参考下面的代码。抱歉回复太长了。

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

        这些是您将获得的值...

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

        【讨论】:

          猜你喜欢
          • 2012-02-15
          • 1970-01-01
          • 2020-11-08
          • 1970-01-01
          • 1970-01-01
          • 2016-03-01
          • 2015-02-15
          相关资源
          最近更新 更多