开发者

Array out of bounds, when I try to swap values

开发者 https://www.devze.com 2023-02-26 07:14 出处:网络
I am trying to swap values in a 2D array, but I get an \"out of bounds\" run time error. What is wrong?

I am trying to swap values in a 2D array, but I get an "out of bounds" run time error.

What is wrong?

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Q1
        int row = 3;
        int colum = 5;

        //Declare 2d array
        int [][] matrix = new int [row][colum];
        //Create array and input values
        Scanner input = new Scanner(System.in);
        System.out.println("Enter " + matrix.length + " rows and "
        + matrix[0].length + " colums: \n");
        for (row = 0; row < matrix.length; row++) {
            for (colum = 0; colum < matrix[row].length ; colum++) {
                matrix[row][colum] = input.nextInt();
        }
            }
        System.out.println("\nMultiplication table");
        System.out.println("--------------------");

        //Print the array
        for (row = 0; row < matrix.length; row++ ) {
            for (colum = 0; colum < matrix[row].length; colu开发者_Python百科m++ ) {
                System.out.printf("%4d", matrix[row][colum]);
            }

            System.out.println();
        }
        //Q2 - Make the values swapable
        Scanner input1 = new Scanner(System.in);
        System.out.println("\n1: Modify a value. ");
        System.out.println("0: Exit the application");
        int selection = input1.nextInt();
        switch(selection) {
            case 0:
                System.exit(0);
                break;
            case 1:
                System.out.println("\nPlease enter the row you would like to change: "  );
                Scanner input2 = new Scanner(System.in);
                int changeRow = input2.nextInt();
                System.out.println("You entered: " + changeRow);
                System.out.println("Please enter the colum you would like to change: "  );
                Scanner input3 = new Scanner(System.in);
                int changeColum = input3.nextInt();
                System.out.println("You entered: " + changeColum);

    error--->   int temp = matrix[row][colum];
                matrix[row][colum] = matrix[changeRow][changeColum];
                matrix[changeRow][changeColum] = temp;

                System.out.println("The value in row " + changeRow + "and colum " + changeColum + "is now changed to " + temp);




                break;

            }
        }
    }


It would have been easier to spot this if you had posted the stack trace of your exception. Here is my guess:

 int temp = matrix[row][colum];

What are the values of row and column here? They are last used before in the loop to print the array, thus you now have row == matrix.length, and column == matrix[row].length (with the row value one before).

Of course, this is outside of the permissible bounds of the array, and you get your IndexOutOfBoundsException.


You are using row and column in your loops. Check the value of them AFTER the loops. I'm not sure if the increment is done and then the < check fails, but if it does, then this

int temp = matrix[row][colum];

is trying to find

matrix[3][5];

and as it starts at 0, that doesn't exists.


When you get changeRow, changeColum inputs you need to create another array initialize with lengths and then assign the values by comparing previous array lengths.

0

精彩评论

暂无评论...
验证码 换一张
取 消