public class MoveZeroes {
public static void main(String a[]) {
int[] intArray = { 1, 0, 9, 7, 6, 0, 2, 0, 7, 0, 0 };
moveZerosToEnd(intArray);
}
private static void moveZerosToEnd(int[] i) {
int counter = 0;
int[] temp = new int[i.length];
for (int k = 0; k < i.length; k++) {
if (i[k] != 0) {
temp[counter++] = i[k];
}
}
while (counter < i.length) {
temp[counter] = 0;
counter++;
}
for (int j : temp) {
System.out.println(j);
}
}
}
while (counter < i.length) {
ReplyDeletetemp[counter] = 0;
counter++;
}
The above snippet may not be needed because the rest of the cells of array "temp" are already assigned with 0 as default value.