简体   繁体   中英

ArrayIndexOutOfBound Exception when reversing array order with multidimensional arrays:

So I'm currently playing around with multidimensional arrays (2D) and I'm trying to reverse the order of each array in a 2-d array.

So I have a 2D-array set as: int firstArray[][] = {{5,6,7,8,9,10}, {11,12,13,14,15,16}}

I have manually looked through the issue to see where I may have went wrong, to see which part of my code would end up going out of bounds in regards to my for-loops. The -1 part also caught me off guard.

I have began doing reverses on a regular 1-d array, and tried to apply the same concept to multidimensional arrays.

class Test2 {

    public static void main (String[] args) {
        int firstArray[][] = {{5,6,7,8,9,10}, {10,11, 12, 13, 14, 15}};
        System.out.println("FIRST ARRAY");
        display(firstArray);
    }

    public void display(int [][]num) {
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < num[i].length/2; j++) {
                int temp = num[i][j];
                num[i][j] = num[i][num.length-1-j];
                num[i][num.length-1-j] = temp;
            }
        }
        for (int a = 0; a < num.length; a++) {
            for (int b = 0; b < num[a].length; b++) {
                System.out.print(num[a][b] + "\t");
            }
        System.out.println();
        }
    }
}

I want the output using my display method to basically be a reverse of the arrays in my 2-d array:

10 9 8 7 6 5

15 14 13 12 11 10

The issue that I'm getting is an

Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: -1 ArrayIndexOutOfBoundsException: -1

at Test2.display(Test2.java:30)

at Test2.main(Test2.java:20)

You are using the length of the wrong dimension.
With num.length you are using the number of rows and not the number of columns of the current row.
You need to change that to num[i].length .

public static void display(int [][]num) {
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < num[i].length/2; j++) {
                int temp = num[i][j];
                num[i][j] = num[i][num[i].length-1-j];
                num[i][num[i].length-1-j] = temp;
            }
        }
        for (int a = 0; a < num.length; a++) {
            for (int b = 0; b < num[a].length; b++) {
                System.out.print(num[a][b] + "\t");
            }
        System.out.println();
        }
}

Notice you wrote num[i][num.length-1-j]; num.length-1-j is basically 2 - 1 -j.

public static void display(int [][]num) {
    for (int i = 0; i < num.length; i++) {
        for (int j = 0; j < num[i].length/2 ; j++) {
            int temp = num[i][j];
            num[i][j] = num[i][num[i].length-1-j];
            num[i][num[i].length-1-j] = temp;
        }
    }
    for (int a = 0; a < num.length; a++) {
        for (int b = 0; b < num[a].length; b++) {
            System.out.print(num[a][b] + "\t");
        }
    System.out.println();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM