Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   arrayIndexOutOfBounds exception (http://www.programmingforums.org/showthread.php?t=13965)

cwl157 Sep 14th, 2007 6:23 PM

arrayIndexOutOfBounds exception
 
I need to make a 4 methods that take a 2d integer array thats 3x3 and look for a 0 and swap it with the number left up right or down from it. I have to make 4 of them for up down left and right. I have the left and up ones working but for some reason i get an arrayIndexOutOfBounds exception when i try it for the right and down ones. The only difference i can see is a +1 instead of a -1 for the position in the 2d array. Here is the method for moving left and for moving right and the printNode() method. Can someone see where the mistake is cause i cant? Thanks.

moveLeft() method
:

// move it left
  public void moveLeft(int[][] state)
  {
      for (int row = 0; row < 3; row++)
      {
        for (int col = 0; col < 3; col++)
        {
            if (state[row][col] == 0)
            {
                state[row][col] = state[row][col-1];
                state[row][col-1] = 0;
                printNode();
            } // end if
        } // end for
      } // end for
  } // end moveLeft


moveRight() method
:

// move it right
  public void moveRight(int[][] state)
  {
      for (int row = 0; row < 3; row++)
      {
        for (int col = 0; col < 3; col++)
        {
            if (state[row][col] == 0)
            {
                state[row][col] = state[row][col+1];
                state[row][col+1] = 0;
                System.out.println("the row is: " + row);
                System.out.println("the col is: " + col);
                printNode();
            } // end if
        } // end for
      } // end for
  } // end moveRight


printNode() method
:

// print the current state
  public void printNode()
  {
      for(int row = 0; row < 3; row++)
      {
        for(int col = 0; col < 3; col++)
        {
          System.out.print(state[row][col] + "  ");
        } // end for
       
        System.out.println();
      } // end for
    } // end printNode


The error happens right before printNode() is called but it still prints the 2d array with the swap made. Also, i have 4 other methods that check to make sure the 0 is in a legal spot to where there is room to the left or right for it to move to. For testing i have the 0 in the middle so it shouldnt be a problem. And you'll notice in the moveRight() method i print the row and col and it matches up.

grumpy Sep 14th, 2007 6:46 PM

Assuming the array (state) is 3 by 3, you should not access elements with indices outside the range [0,2]. Which means that accessing state[i][j] implies a condition that both i and j are in the range [0,2].

In your MoveLeft() method, col-1 will be -1 if col is 0, which is out of the range [0,2].

In your MoveRight() method, col+1 will be 3 if col is 2, which is out of the range [0,2].

cwl157 Sep 14th, 2007 6:58 PM

but the only time i should get the error for move left is if the 0 is say positioned like this:

[1, 2, 3
0, 4, 5
6, 7, 8]

if it was like this i cant move left because i would be replacing the spot at -1 which isnt allowed same for the right if its set up like this:

[1, 2, 3
4, 5, 0
6, 7, 8]

then i shouldnt be able to move right because i would be accessing 3 which like you said is out of range, but my test is like this

[1, 2, 3
4, 0, 5
6, 7, 8]

so this should not be a problem for replacing 1 to the right of 0 with 0 because it should still be in range but for some reason its saying its not.

cwl157 Sep 14th, 2007 7:18 PM

alright i figured it out. It is going through the 2d array from top left to bottom right, so therefore if i move it either left or up then its no problem cause its past those but if i move it right or down then it hits the 0 again as it goes through the loop. This helped answer a question that i had though and that was where do i want to put the checks to make sure it can be moved and the answer is right before i do it inside the moveRight() method. But now i have another problem because i want it to move one at a time so its like right after i move it i have to break out of both loops, or maybe just end the method entirely?

EDIT: Yep i just ended the method right after i make the first swap. Since the only purpose of the method is to move the 0 once in the specified direction, once i move it once theres no reason to let the loop go through the rest of the 2d array, so i just return it.


All times are GMT -5. The time now is 3:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC