Java Array Swap swapZeros(int[] input, int zeroCount)

Here you can find the source of swapZeros(int[] input, int zeroCount)

Description

swap Zeros

License

Apache License

Declaration

static void swapZeros(int[] input, int zeroCount) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    static void swapZeros(int[] input, int zeroCount) {
        int inputLength = input.length;
        int nonZeroLength = inputLength - zeroCount;

        for (int i = 0; i < nonZeroLength; i++) {
            if (inputLength <= nonZeroLength) {
                break;
            }//from   w w w. j a  v a 2s  .c  om

            int element = input[i];
            if (element == 0) {
                int temp = 0;
                while (temp == 0) {
                    temp = input[--inputLength];
                    input[inputLength] = 0;
                }
                input[i] = temp;
            }
        }
    }
}

Related

  1. swapPerTowElement(int[] arr)
  2. swapReferences(Object[] a, int index1, int index2)
  3. swapRgbColorComponents(byte[] pixelArray)
  4. swapTokens(String grid[])
  5. swapValues(double[] arr, double v1, double v2)