I have a 2d array like this:
2 0 0 2 0 4
2 0 0 2 0 4
2 0 0 2 0 4
And I want to shift all the zeros to the left, so for that I made this method:
public static void shiftLeft(int [][] array){
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array.length - 1; i++) {
if ((array[j][i] != 0) && (array[j][i + 1] == 0)) {
array[j][i + 1] = array[j][i];
array[j][i] = 0;
}
}
}
}
But the output I get is this:
0 0 2 0 2 4
0 0 2 0 2 4
0 0 2 0 2 4
How can I make all zeros to go to the left?