-7

I created a 2D array (a Cell[][] grid) and each cell contains an ArrayList with the name neighborCells; and now I am trying to find its neighbour cells but I am getting the IndexOutOfBoundsException. Can you help?

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    
            }
        }   
    }
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
flower
  • 121
  • 2
  • 14
  • How is ArrayList defined? Is it the .NET class or the Java class? – Wolf Jun 06 '14 at 06:12
  • Ok, sorry for having messed your answer. You edited it again. I think it isn't correct now. Isn't the `neighborCells` the collection of neighbours you want to get? – Wolf Jun 06 '14 at 06:18
  • yes the neighborCells is the ArrayList that contains the neighbors of the grid. Each grid[i][j] has inside an arraylist of its neighbors. – flower Jun 06 '14 at 06:22
  • Meanwhile my fast (but wrong!) first answer was deleted. Please check if the new one is acceptable to you. I tried the code in C++,and it does what it's supposed to do. – Wolf Jun 06 '14 at 14:09
  • it doesn't work for me though. i did what you recommened below and i get the error neighnorCells has private access in Cell(in the 2nd change you proposed.) `public class Cell` has a `private ArrayList neighborCells; ` inside. – flower Jun 06 '14 at 14:20
  • See my comment below, and read a good Java beginners tutorial. Good luck! I'm off then... – Wolf Jun 06 '14 at 14:30

2 Answers2

3

I found[1] three errors in your example and suggest the following corrections:

1) change the calculation of end positions - which caused your exception - into this

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

2) you need an arraylist for each cell, so change the initial creation of the results down two lines and change it to

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

3) a cell isn't its own neighbour

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

[1] I ported it to C++, so sorry if there are any mistakes left from porting it back ;)

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • for the 2) i get the error neighborCells has private access in Cell – flower Jun 06 '14 at 13:52
  • @flower welcome back! I think you, as a developer, have write access to the definition of the `Cell` class: *One* (quick) option is to add `public` before the member definition. – Wolf Jun 06 '14 at 14:00
  • yes i have write access to Cell class. But it is already public class Cell and inside it has a private ArrayList neighborCells; – flower Jun 06 '14 at 14:06
  • That I expected. Try (1) either to change this into `public ArrayList neighborCells;` or (2) add a `public void addNeighbor(Cell cell)` function to it. Sadly I cannot suggest a good beginner's tutorial for Java (I'm not a Java programmer), but I guess you are in need for it. (Just one note: the first option is considered to be "bad style") – Wolf Jun 06 '14 at 14:17
1

Here is what I used for a test Cell class since you didn't provide yours

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

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

    private List<Cell> list;
}

And here is an example that will find the neighbouring cells. I'm not sure why you have two more nested for loops if there can only be 4 neighbouring cells (Above, below, left and right). It seems a simpler to me to just get rid of them

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] );
    }
}
TFischer
  • 1,278
  • 2
  • 24
  • 42