|
| 1 | +package easyq; |
| 2 | +/* BY-M.NAVEEN |
| 3 | + * engineerscodes |
| 4 | + * code -Program for array rotation |
| 5 | + * sr.code-Ar2 |
| 6 | + * moves zeros to end of the array |
| 7 | + * */ |
| 8 | +import java.util.*; |
| 9 | +public class Move_all_zeroes_to_end // class name |
| 10 | +{ static Scanner nav=new Scanner(System.in); // calling Scanner and creating object nav |
| 11 | + static int array[],t[],size; // class instances variables |
| 12 | + public Move_all_zeroes_to_end(int c) // constructor |
| 13 | + { |
| 14 | + size=c; |
| 15 | + array=new int[size]; //intialization of variables |
| 16 | + t=new int[size]; |
| 17 | + } |
| 18 | + |
| 19 | + public static void move() //method to move zero |
| 20 | + { int j=0; |
| 21 | + for(int i=0;i<size;i++) // finding zeros in array |
| 22 | + { |
| 23 | + if(array[i]!=0) //if array element is not equal then the elements is write in temp array(t) |
| 24 | + t[j++]=array[i]; |
| 25 | + } |
| 26 | + ///for( ;j<size;j++)// This is not needed because when array is initialization all element are zero |
| 27 | + //// t[j]=0;// so the element at last are zeros |
| 28 | + array=t.clone(); |
| 29 | + //System.arraycopy(t,0,array,0,size);//can use clone fuction also array=t.clone() |
| 30 | + //array=t; can use this to but this will reference t and array so,the changes in t will reflect array[]; |
| 31 | + System.out.println(" After moving zeros "+Arrays.toString(array)); //printing array After moving zeros |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + public static void input() // method to enter element in array |
| 36 | + { |
| 37 | + for(int i=0;i<array.length;i++) |
| 38 | + array[i]=nav.nextInt(); |
| 39 | + System.out.println("element in array are::"+Arrays.toString(array)); |
| 40 | + move(); //calling move method |
| 41 | + } |
| 42 | + public static void main(String arg[]) // main method |
| 43 | + { |
| 44 | + System.out.println("enter the size of the array"); |
| 45 | + Move_all_zeroes_to_end n=new Move_all_zeroes_to_end (nav.nextInt()); |
| 46 | + System.out.println("Enter the element of array"); |
| 47 | + input(); // calling input method |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments