Advertisement
Guest User

doe

a guest
Oct 31st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class exercise200 {
  2.  
  3. public static void main(String args[]) {
  4.  
  5. System.out.println("Entre the size of the array");
  6. int length = ITI1120.readInt();
  7. System.out.println("Entre the values");
  8. int[] nums = new int[length];
  9.  
  10. for(int i=0; i<nums.length; i++){
  11. nums[i]= ITI1120.readInt();
  12. }
  13. //int[] tmp = new int[length];
  14. printArray(nums);
  15. Zeros(nums);
  16. System.out.println();
  17. printArray(nums);
  18.  
  19. }
  20. public static void Zeros(int[] arr){
  21. int count = 0;
  22.  
  23. for (int i = 0; i < arr.length; i++)
  24. if (arr[i] != 0)
  25. arr[count++] = arr[i]; // here count is incremented
  26.  
  27. while (count < arr.length)
  28. arr[count++] = 0;
  29.  
  30. }
  31.  
  32.  
  33.  
  34. public static void printArray(int[] array)
  35. {
  36. for(int i=0; i<array.length; i++)
  37. System.out.print(array[i] + " ,");
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement