Rearranging array elements depending on their indexes
The trick you are missing here is a simple swap.

All you need to do is keep a "fast" and a "slow" index in the array. The "fast" index scans forwards looking for 0 values. The "slow" index is an insert-point for where the next 0 would belong.

Consider this loop:

public int[] zeroFront(int[] nums){

    int slow = 0; // next zero inserted here
    for (int fast = 0; fast &lt; nums.length; fast++) {
        if (nums[fast] == 0) {
            nums[fast] = nums[slow];
            nums[slow] = 0;
            slow++;
        }
    }
    return nums;
}


That loop finds all zero values and then swaps non-zero values with them from the beginning of the loop, ending with a situation where the zeros are all at the front.

Sometimes, tricks like these are really hard to spot, but, when pointed out, make you think: Neat!