簡體   English   中英

在arrayList中存儲二維數組元素

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

我有一個二維的整數網格。

grid[][];

假設我從2d數組中隨機得到一個元素。 我的目的是返回其相鄰的網格元素。 為此,我創建一個ArrayList

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

在很多情況下,我將不得不去做,而在每種情況下,neighborSide的數量將有所不同。 所以我選擇的數據結構是ArrayList

但是當我將元素添加到列表中時

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

我知道這是錯誤的,因為我是將grid元素的值添加到ArrayList而不是元素本身。 有誰知道如何將arrayElements存儲在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對象是一個二維整數數組。 grid[row][column+1]是一個整數,位於網格中的各個索引中。

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

將不起作用,因為您要向二維int數組的ArrayList列表中添加int 我相信您想存儲數字,並且想知道這些數字是什么。 我想知道鄰居的定義。 在這里,我將假設鄰居是位於當前元素上,下,左或右的元素,或者,更科學地說,這些元素與Taxicab-geometry中的當前元素之間的精確距離為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();
    }

}

現在,如果您使用該類,則每個元素都會知道它們的鄰居。 您可以像這樣初始化整個事情:

GridHandler.init(grid);

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM