0

I apologize if this has been posted before, however I could not find anything specifically related to my problem.

I have a small bit of my code here, I have a 2D array with some information, and I loop through the rooms and columns as shown. This works, and everything is printed out, but I get this error at the end of the loop:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Main.main(Main.java:14)

Here is my code:

    public class Main {
    public static void main(String[] args){
        int data[][] = {{1, 1, 1, 1, 1, 0, 0},
                        {0, 0, 0, 0, 0, 0, 0},
                        {1, 1, 1, 1, 1, 0, 0},
                        {1, 1, 1, 1, 1, 0, 0},
                        {1, 1, 1, 1, 1, 0, 0},
                        {1, 1, 1, 1, 1, 0, 0}};

        int x;
        int y;
        for(int i = 0; i < data.length; i++){
            for(int j = 0; j < data[j].length; j++){
                x = j * 16;
                y = i * 16;
                System.out.println(x + " " + y + " " + data[i][j]);
            }
        }
    }
}

What is the issue here?

2 Answers 2

3

It should be for(int j = 0; j < data[i].length; j++)

Sign up to request clarification or add additional context in comments.

Comments

0

Reference ::

int[][] data = {{1, 2}, {3, 4, 5}};
System.out.println(data.length + ", " + data[0].length + ", " + data[1].length);


Output= 2, 2, 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.