javatechie

Shift All Zero To End

Sep 21st, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.49 KB | None | 0 0
  1. package com.javatechie.solution;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class ShiftAllZeroToEnd {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         int[] array={4,32,0,1,0,5,0,7,8,9,0} ;
  10.  
  11.         int count=0;
  12.         int len=array.length-1;
  13.  
  14.         for (int i:array){
  15.             if(i!=0){
  16.                 array[count++]=i;
  17.             }
  18.         }
  19.         while (count<len){
  20.             array[count++]=0;
  21.         }
  22.         System.out.println(Arrays.toString(array));
  23.     }
  24. }
Add Comment
Please, Sign In to add comment