Question: public static int [][] moveDown(int [][] grid) { for(int i = 0; i < grid.length; i--) { for(int j = 0; j < grid.length; j++)

public static int [][] moveDown(int [][] grid) {

for(int i = 0; i < grid.length; i--) {

for(int j = 0; j < grid.length; j++) {

int currentValue = grid[i][j];

if(currentValue != 0) {

if(i !=0) {

for(int k = i+1; k >= 0; k++) {

if(grid[j][k] != 0) {

if(currentValue == grid[j][k]) {

grid[j][k] = 2 * currentValue;

currentValue = grid[j][k];

grid[k+1][j] = 0;

}

} else {

grid[j][k] = currentValue;

grid[k+1][j] = 0;

}

}

}

}

}

}

This is my code and i get the following error..

can you please explain how can i fix it.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4

at Game1024.moveDown(Game1024.java:110)

at Game1024.main(Game1024.java:193)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!