0

I have been trying to fill my

 int[][][] sudoku = new int[5][9][9]; 

array with

String[][] tmp = new String[21][21];

tmp[][] holds numbers like

005700020009600020 
490060010140050030

For example this code works perfect and it gives me the number I want

System.out.println(Integer.valueOf(tmp[4][10]));

But this code

//sudoku1
        b=0; c=0;
        for (int i = 0; i < 6; i++) {
            for (int j = 9; j < 18; j++) {
                sudoku[1][b][c] = Integer.valueOf(tmp[i][j]);
            c++;
            }
            b++;
        }

throws "Index 9 out of bounds for length 9" error

1 Answers1

1

You have c defined outside both loops, and added 1 each execution in the inner loop. The inner loop gets executed 6*9 times, so c can reach even the value 54, but it throws exception when it reaches 9 when trying to use sudoku[1][b][c], as the array indexes for the third element of the multiarray sudoku go from 0 to 8.

gmanjon
  • 1,483
  • 1
  • 12
  • 16
  • Yes, it solved my problem. I don't really understand how I couldn't see this. Thank you good sir! – Ersin Yılmaz Aslan Nov 19 '21 at 21:52
  • 1
    You are most welcome. Don't worry, it happens to everyone. Once I spent half an hour wondering why my code didn't enter in my loop with `myVar == "S"`, when I knew for sure that the value of `myVar` was definately `"S"` xDDD – gmanjon Nov 19 '21 at 21:56