Home > Software design >  2D array filling | ArrayIndexOutOfBoundsException error
2D array filling | ArrayIndexOutOfBoundsException error

Time:11-11

good afternoon! hi all! 1st time posting

for my assignment we are filling arrays using arithmetic and nested for loops. i've done a complete filling of a 2D array before using prime numbers, although i think i'm messing up somewhere..

when doing the line int priorNum = arr[r-1][c]; (see full code below) i run into an exception. i am trying to overwrite other lines in my array with this new equation, but must i be stopped by this utmost unchivalrous java error.

the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10

my array: int[][] arrayDimension = new int[10][6];

public static void populate2D (int[][] arr) {
        //hardcode in values first :)
        
        //and then peek up one row, but you can't go above the original row
        
        arr[0][1] = 10;
        arr[0][2] = 100;
        arr[0][3] = 500;
        arr[0][4] = 1000;
        arr[0][5] = 5000;

        int count = 0;
        //for each row..
        for (int r = 0; r < arr.length; r  ) { //for each row
            
            for ( int c = 0; c < arr[r].length; c  ) { //for each column
                arr[r][0] = count;
                //never navigate out of bounds
                //row 0 is where we're at.. how to populate further rows..?
                
                int priorNum = arr[r-1][c];
                int nextNum = priorNum * 2;
                arr[r][c] = nextNum;

               //can't look back .. SO go UP one.. which is r - 1 goes back one.. and then the length goes - 1
                //when c is - peek UP a row <  and enter last column.. ^
            
            }
            count  ;
            
        }

}

i left in some notes that i wrote if you can understand what i'm trying to go for :)

i can also offer this printArray method i wrote for any testing you'd like to try!

    public static void print2DArray(int[][] arr) {
        
        
        for ( int r = 0; r < arr.length; r  ) {
            
            for ( int c = 0; c < arr[r].length; c  ) {
                
                System.out.print(arr[r][c]   "\t");
            }
            
            System.out.println();
        }
            
    }
    
}

thank you for any replies / assistance! everyone here seems very nice, i could not find my type of question that deals with my answer so i felt bad about posting hehe

CodePudding user response:

The problem I can see is that in the first iteration when int priorNum = arr[r-1][c]; gets executed, r = 0, as specified by your outer for loop.

So you are basically trying to access an element of your 2D array using a negative index, which will result in an ArrayIndexOutOfBoundException being thrown.

You could adopt an if statement that will handle the first iteration so that you will not access a prior index.

You could also look at the Array access section of the following article: https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

Hope this helped.

  • Related