Java 如何将数组添加到ArrayList?
我有一个int[3][3]数组,它只包含0或1个值, 如果值为1,我想在ArrayList中将该值的坐标添加为int[2]数组,但我不知道为什么它总是添加最后一个1值坐标,有什么问题Java 如何将数组添加到ArrayList?,java,arrays,arraylist,add,Java,Arrays,Arraylist,Add,我有一个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 Arr
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();
}
}
您需要为要放置在列表中的每个i,j对创建新的坐标数组。现在,您要多次放置同一数组,以记住最后一对集合
换句话说,你需要
if (board[i][j] == 1) {
coordinates = new int[2];//add this line
coordinates[0] = i;
coordinates[1] = j;
arrayList.add(coordinates);
}
您需要为要放置在列表中的每个i,j对创建新的坐标数组。现在,您要多次放置同一数组,以记住最后一对集合
换句话说,你需要
if (board[i][j] == 1) {
coordinates = new int[2];//add this line
coordinates[0] = i;
coordinates[1] = j;
arrayList.add(coordinates);
}
每次添加坐标对象时,都需要创建新的坐标对象:
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} );
}
否则,将多次添加同一对象,并每次对其进行修改,以便只保留最后一个坐标
如果您养成了使用窄范围临时变量的习惯,这通常是很自然的,因为您不会在循环外部拖动状态。每次添加坐标对象时,您都需要创建一个新的坐标对象:
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} );
}
否则,将多次添加同一对象,并每次对其进行修改,以便只保留最后一个坐标
如果您养成了使用窄范围临时变量的习惯,这通常是很自然的,因为您不会在循环外部拖动状态。如果要将点放入ArrayList,我建议您创建一个采用坐标的点对象。请参考下面的代码。对不起,回复太长了
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
}
}
如果要将点放入ArrayList,我建议您创建一个获取坐标的点对象。请参考下面的代码。对不起,回复太长了
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
}
}