简体   繁体   中英

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. My aim is to return its adjacent grid elements. For that I am creating an 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. So my choice of data structure is an 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. Does anyone have any idea on how to store the arrayElements in the arrayList and not the value stored in them ?? 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:

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[row][column+1] is an integer, located in the respective indexes in your grid.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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