简体   繁体   English

在arrayList中存储二维数组元素

[英]Storing a 2d array elements in an arrayList

I have a 2d grid of integers. 我有一个二维的整数网格。

grid[][];

Suppose I am given an element randomly from the 2d array. 假设我从2d数组中随机得到一个元素。 My aim is to return its adjacent grid elements. 我的目的是返回其相邻的网格元素。 For that I am creating an ArrayList 为此,我创建一个ArrayList

ArrayList<int[][]> adjacentSidesList = new ArrayList<int[][]>();

I would have to go for quite a few number of cases and in each case the number of the adjacentSides would be different. 在很多情况下,我将不得不去做,而在每种情况下,neighborSide的数量将有所不同。 So my choice of data structure is an ArrayList 所以我选择的数据结构是ArrayList

But when I would add an element to the list 但是当我将元素添加到列表中时

adjacentSidesList.add(grid[row][column+1]); 

I understand this is wrong because I am adding the value of the grid element to the ArrayList and not the element itself. 我知道这是错误的,因为我是将grid元素的值添加到ArrayList而不是元素本身。 Does anyone have any idea on how to store the arrayElements in the arrayList and not the value stored in them ?? 有谁知道如何将arrayElements存储在arrayList中而不是存储在它们中的值吗? Any alternate method is also welcome with the reasons why the method is better 也欢迎使用任何其他替代方法,以说明该方法更好的原因

Thanks in Advance 提前致谢

You could create a new class which will hold row and column index of 2D array element like: 您可以创建一个新类,该类将保存2D数组元素的行和列索引,例如:

class Index {
    private int row;
    private int column;
    //getter and setters
}

Now when you want to store the data in list, you store the index object and when you have to access the element, you can access it like: 现在,当您要将数据存储在列表中时,将存储索引对象,并且当您必须访问元素时,可以像以下方式访问它:

Index index = adjacentSidesList.get(0);
int element = grid[index.getRow()][index.getColumn()];

Your grid object is a two-dimensional integer array. 您的grid对象是一个二维整数数组。 grid[row][column+1] is an integer, located in the respective indexes in your grid. grid[row][column+1]是一个整数,位于网格中的各个索引中。

adjacentSidesList.add(grid[row][column+1]);

will not work, because you want to add an int to a list of ArrayList of two-dimensional int arrays. 将不起作用,因为您要向二维int数组的ArrayList列表中添加int I believe you want to store numbers and you want to know what are those numbers. 我相信您想存储数字,并且想知道这些数字是什么。 I wonder about the definition of neighbor. 我想知道邻居的定义。 I will suppose here that the neighbor is the element located up, down, left or right to the current element, or, to put it more scientifically, the elements being located exactly at a distance of 1 from the current element in Taxicab-geometry . 在这里,我将假设邻居是位于当前元素上,下,左或右的元素,或者,更科学地说,这些元素与Taxicab-geometry中的当前元素之间的精确距离为1。

The first problem is that a point might be at the margin of your space, which would mean they do not have a neighbor. 第一个问题是一点可能在您的空间边缘,这意味着他们没有邻居。 The next problem is a general formula for the neighbors. 下一个问题是邻居的一般公式。 I believe your numbers should be aware of their position, therefore we should define the following class: 我相信您的电话号码应该知道其位置,因此我们应该定义以下类别:

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

}

Now, if you use that class, each element will be aware of their neighbors. 现在,如果您使用该类,则每个元素都会知道它们的邻居。 You can initialize the whole thing like this: 您可以像这样初始化整个事情:

GridHandler.init(grid);

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM