0

i'm using this code to insert in 2d array but i keep geeting exception, any ideas?

int rows=4, columns=4, value=0;
    int[][] arySwap = new int[rows][columns];
    for (int i=0 ; i<arySwap[rows].length ; i++){
        for (int j=0 ; j<arySwap[columns].length ; j++){
            value = value+1;
            arySwap[i][j]= value;
        }
    }
  • 1
    `arySwap[rows].length` -> `arySwap[columns].length` ... so the first time the outer array represents the rows, then something magical happens and then it represents the columns? What kind of magic is that? – Tom Mar 15 '17 at 11:53

1 Answers1

0

You try to access the fourth value of arySwap with arySwap[column] and also the fouth value with arySwap[column]. Change your code like this:

  int rows=4, columns=4, value=0;
        int[][] arySwap = new int[rows][columns];
        for (int i=0 ; i<rows ; i++){
            for (int j=0 ; j<arySwap[i].length ; j++){
                value = value+1;
                arySwap[i][j]= value;
            }
        }
Markus
  • 1,111
  • 1
  • 8
  • 23