False валю устраивает вроде...
package com.javarush.task.task35.task3513;
public class Main {
public static void main(String[] args) {
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = arr.length;
pushZerosToEnd(arr, n);
System.out.println("Array after pushing zeros to the back: ");
for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
// int[] array = {0, 1, 0, 0};
// test(array);
// }
// private static void test(int[] tiles) {
// if (tiles[0] == 0 && tiles[1] == 0 && tiles[2] == 0) {
// tiles[0] = tiles[3];
// tiles[3] = 0;
// }
// if (tiles[1] == 0 && tiles[2] == 0) {
// tiles[1] = tiles[3];
// tiles[3] = 0;
// }
// if (tiles[0] == 0 && tiles[1] == 0) {
// tiles[0] = tiles[2];
// tiles[1] = tiles[3];
// tiles[2] = 0;
// tiles[3] = 0;
// }
// for (int i = 0; i < 3; i++) {
// if (tiles[i] == 0) {
// int temp = tiles[i + 1];
// tiles[i] = temp;
// tiles[i + 1] = 0;
// }
// }
//
static void pushZerosToEnd(int arr[], int n)
{
int count = 0; // Count of non-zero elements
// Traverse the array. If element encountered is
// non-zero, then replace the element at index 'count'
// with this element
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i]; // here count is
// incremented
// Now all non-zero elements have been shifted to
// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}
}