【问题标题】:Trying to find neighbour cells in a 2D array and getting IndexOutOfBounds尝试在二维数组中查找相邻单元格并获取 IndexOutOfBounds
【发布时间】:2014-07-27 08:02:12
【问题描述】:

我创建了一个二维数组(Cell[][] 网格),每个单元格都包含一个名为 neighborCellsArrayList;现在我正试图找到它的邻居单元,但我得到了IndexOutOfBoundsException。你能帮忙吗?

ArrayList<Cell> neighborCells = new ArrayList();
for(int i = 0; i < grid.length; i++){
    for(int j = 0; j < grid.length; j++) { //we can also use grid.length since it is the same
        int startPositionX = (i - 1 < 0) ? i : i - 1;
        int startPositionY = (j - 1 < 0) ? j : j - 1;
        int endPositionX =   (i + 1 > grid.length) ? i : i + 1;
        int endPositionY =  (j + 1 > grid.length) ? j : j + 1;
        for (int row = startPositionX; row <= endPositionX; row++) { 
            for (int col = startPositionY; col <= endPositionY; col++) {
                neighborCells.add(grid[row][col]); // here is the error    
            }
        }   
    }
}

【问题讨论】:

  • ArrayList 是如何定义的?是 .NET 类还是 Java 类?
  • 好的,很抱歉弄乱了你的答案。你又编辑了。我认为现在不正确。 neighborCells不是你想得到的邻居集合吗?
  • 是的,neighborCells 是包含网格邻居的 ArrayList。每个 grid[i][j] 都有一个它的邻居数组列表。
  • 同时我快速(但错误!)的第一个答案被删除了。请检查您是否可以接受新的。我尝试了 C++ 中的代码,它完成了它应该做的事情。
  • 它对我不起作用。我做了你在下面推荐的事情,我得到了错误 neighnorCells has private access in Cell(在你提出的第二个更改中。)public class Cell 里面有一个private ArrayList&lt;Cell&gt; neighborCells;

标签: java arrays multidimensional-array indexoutofboundsexception neighbours


【解决方案1】:

我在您的示例中发现[1]三个错误,并提出以下更正建议:

1) 将结束位置的计算 - 导致您的异常 - 更改为此

int endPositionX =   (i + 1 >= grid.length) ? i : i + 1;
int endPositionY =  (j + 1 >= grid.length) ? j : j + 1;

2) 每个单元格都需要一个数组列表,因此将结果的初始创建向下更改两行并将其更改为

grid[i][j].neighborCells = new ArrayList();

3) 一个单元格不是它自己的邻居

if (row!=i || col!=j) {
    grid[i][j].neighborCells.add(grid[row][col]);
}

[1] 我将它移植到 C++,如果移植回来有任何错误,请见谅 ;)

【讨论】:

  • 对于 2) 我收到错误 neighborCells has private access in Cell
  • @flower 欢迎回来!我认为,作为开发人员,您对 Cell 类的定义具有写入权限:一个(快速)选项是在成员定义之前添加 public
  • 是的,我对 Cell 类有写权限。但它已经是公共类 Cell 并且里面有一个私有 ArrayList neighborCells;
  • 我所期望的。尝试 (1) 将其更改为 public ArrayList&lt;Cell&gt; neighborCells; 或 (2) 向其添加 public void addNeighbor(Cell cell) 函数。遗憾的是,我不能推荐一个好的 Java 初学者教程(我不是 Java 程序员),但我猜你需要它。 (请注意:第一个选项被认为是“坏风格”)
【解决方案2】:

这是我用于测试 Cell 类的,因为你没有提供你的

public class Cell {
    public Cell( ) {
        list = new ArrayList<Cell>( );
    }

    public void add( Cell cell ) {
        list.add( cell );
    }

    private List<Cell> list;
}

这是一个查找相邻单元格的示例。如果只能有 4 个相邻单元格(上、下、左和右),我不确定为什么还要再嵌套两个 for 循环。摆脱它们对我来说似乎更简单

Cell[][] grid = new Cell[5][5];

for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[i].length; j++) {
        cell[i][j] = new Cell( );
    }
}

for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[i].length; j++) {
        // Cell above
        if (i > 0) cell[i][j].add( cell[i - 1][j] );

        // Cell to the left
        if (j > 0) cell[i][j].add( cell[i][j - 1] );

        // Cell below
        if (i < grid.length - 1) cell[i][j].add( cell[i + 1][j] );

        // Cell to the right
        if (j < grid[i].length - 1) cell[i][j].add( cell[i][j + 1] );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2011-01-03
    • 2021-10-28
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    相关资源
    最近更新 更多