Where is this going wrong? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

StackOverflow https://stackoverflow.com/questions/20553297

  •  01-09-2022
  •  | 
  •  

Question

I am trying to count the number of Space objects in a Space[][], of a particular colour. When I use this method to count the number of objects of a particular colour in a row, it works fine:

public int countRowWhite(Space[][] board, int row)//TESTED//when counting in a row, the columns         go up the row stays the
//same,THIS GOES THROUGH THE ROW ADDING UP NUMBERS OF WHITES
{
    int count = 0;
    for(int column=0; column<board.length;column++)
    {
        if((board[row][column]).getColour().equals(spaceColour.White))
        {
            count+=1;
        }
    }
    return count;
}

However when I try this method, to count the number of objects in a column, I get an exception:

public int countColumnWhite(Space[][] board, int column)//when counting in a row, the columns go up the row stays the
//same,THIS GOES THROUGH THE ROW ADDING UP NUMBERS OF WHITES
{
    int count = 0;
    for(int row =0; column<board.length;row++)
    {
        if((board[row][column]).getColour().equals(spaceColour.White))
        {
            count+=1;
        }
    }
    return count;
}

I call both of these methods in the following test method:

public void testMethods()
   {
           Space[][] test = new Space[5][5];

   for(int i = 0; i < test.length; i++){
         for(int j = 0; j < test.length; j++){
              test[i][j] =  new Space(spaceColour.Null); 
         }
    }


   test[0][1].setColour(spaceColour.White); 
   test[0][2].setColour(spaceColour.Black); 
   test[2][1].setColour(spaceColour.Black); 
   test[2][2].setColour(spaceColour.Black); 

   System.out.println(countColumnWhite(test, 0));





   for(int row= 0; row<test.length;row++)
   {
       for(int column = 0; column<test.length;column++)
       {
           if (test[row][column].getColour().equals(spaceColour.White))
           {
               System.out.println("Whites at row: " + row + " and Column: "+ column);
           }
       }
   }

If it helps, the exception is always equal to the number of rows and columns the 2d array 'test' has

Was it helpful?

Solution

I imagine that this line:

for(int row =0; column<board.length;row++)

should be:

for(int row = 0; row < board.length; row++)

Your terminating condition was checking that column is lesser than board.length, when it should be checking that row is lesser than board.length. You keep incrementing row, but the termination condition is never true, so you end up going outside the bounds of the array.

Another thing is that your code implicitly assumes that you are working with a square matrix (i.e 2-d array same number of rows and columns). So if you have unequal rows and columns, you will run into the same issue. If your assumption is valid, then this is fine. I imagine this is some kind of game board that is supposed to be square.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top