Java 在arrayList中存储二维数组元素
我有一个二维整数网格Java 在arrayList中存储二维数组元素,java,arrays,arraylist,Java,Arrays,Arraylist,我有一个二维整数网格 grid[][]; 假设我从2d数组中随机获得一个元素。我的目标是返回其相邻的栅格元素。 为此,我正在创建一个ArrayList ArrayList<int[][]> adjacentSidesList = new ArrayList<int[][]>(); 我理解这是错误的,因为我将网格元素的值添加到ArrayList中,而不是元素本身。有人知道如何将arrayElements存储在arrayList中,而不是存储在arrayList中的值吗?
grid[][];
假设我从2d数组中随机获得一个元素。我的目标是返回其相邻的栅格元素。
为此,我正在创建一个ArrayList
ArrayList<int[][]> adjacentSidesList = new ArrayList<int[][]>();
我理解这是错误的,因为我将网格元素的值添加到ArrayList中,而不是元素本身。有人知道如何将arrayElements存储在arrayList中,而不是存储在arrayList中的值吗??
也欢迎任何替代方法,并说明该方法更好的原因
提前感谢您可以创建一个新类,该类将保存2D数组元素的行和列索引,如:
class Index {
private int row;
private int column;
//getter and setters
}
现在,当您想在列表中存储数据时,您可以存储索引对象,当您必须访问元素时,您可以像以下那样访问它:
Index index = adjacentSidesList.get(0);
int element = grid[index.getRow()][index.getColumn()];
您的
网格
对象是一个二维整数数组grid[row][column+1]
是一个整数,位于网格中相应的索引中
adjacentSidesList.add(grid[row][column+1]);
将不起作用,因为您要将int添加到二维int
数组的ArrayList
列表中。我相信你想储存数字,你想知道这些数字是什么。我想知道邻居的定义。我将在这里假设,相邻元素是位于当前元素上、下、左或右的元素,或者更科学地说,元素正好位于距离当前元素1的位置
第一个问题是,一个点可能在你的空间边缘,这意味着他们没有邻居。下一个问题是邻域的一般公式。我相信您的数字应该知道它们的位置,因此我们应该定义以下类别:
public class GridHandler {
private static GridHandler[][] grid;
private int i;
private int j;
private int value;
public static void init(int[][] input) {
int rowNumber = input.length;
int columnNumber = input[0].length;
grid = new GridHandler[rowNumber][columnNumber];
for (int r = 0; r < rowNumber; r++) {
for (c = 0; c < columnNumber; c++) {
grid[r][c] = new GridHandler(r, c, input[r][c]);
}
}
}
public static GridHandler[][] getGrid() {
return grid;
}
public GridHandler(int i, int j, int value) {
this.i = i;
this.j = j;
this.value = value;
grid[i][j] = this;
}
public int getValue() {
return value;
}
public void setValue(value) {
this.value = value;
}
public int getLeftValue() throws ArrayIndexOutOfBoundsException {
if (j == 0) {
throw new ArrayIndexOutOfBoundsException("Left edge");
}
return grid[i][j - 1].getValue();
}
public int getUpValue() throws ArrayIndexOutOfBoundsException {
if (i == 0) {
throw new ArrayIndexOutOfBoundsException("Up edge");
}
return grid[i - 1][j].getValue();
}
public int getRightValue() throws ArrayIndexOutOfBoundsException {
if (j == grid[0].length - 1) {
throw new ArrayIndexOutOfBoundsException("Right edge");
}
return grid[i][j + 1].getValue();
}
public int getDownValue() throws ArrayIndexOutOfBoundsException {
if (i == grid.length - 1) {
throw new ArrayIndexOutOfBoundsException("Down edge");
}
return grid[i + 1][j].getValue();
}
}
我希望这会有所帮助。如果要返回的是与给定数字相邻的所有整数,则要返回的是整数列表。不是整数数组的数组列表。
public class GridHandler {
private static GridHandler[][] grid;
private int i;
private int j;
private int value;
public static void init(int[][] input) {
int rowNumber = input.length;
int columnNumber = input[0].length;
grid = new GridHandler[rowNumber][columnNumber];
for (int r = 0; r < rowNumber; r++) {
for (c = 0; c < columnNumber; c++) {
grid[r][c] = new GridHandler(r, c, input[r][c]);
}
}
}
public static GridHandler[][] getGrid() {
return grid;
}
public GridHandler(int i, int j, int value) {
this.i = i;
this.j = j;
this.value = value;
grid[i][j] = this;
}
public int getValue() {
return value;
}
public void setValue(value) {
this.value = value;
}
public int getLeftValue() throws ArrayIndexOutOfBoundsException {
if (j == 0) {
throw new ArrayIndexOutOfBoundsException("Left edge");
}
return grid[i][j - 1].getValue();
}
public int getUpValue() throws ArrayIndexOutOfBoundsException {
if (i == 0) {
throw new ArrayIndexOutOfBoundsException("Up edge");
}
return grid[i - 1][j].getValue();
}
public int getRightValue() throws ArrayIndexOutOfBoundsException {
if (j == grid[0].length - 1) {
throw new ArrayIndexOutOfBoundsException("Right edge");
}
return grid[i][j + 1].getValue();
}
public int getDownValue() throws ArrayIndexOutOfBoundsException {
if (i == grid.length - 1) {
throw new ArrayIndexOutOfBoundsException("Down edge");
}
return grid[i + 1][j].getValue();
}
}
GridHandler.init(grid);