Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Ex15 {
  2.  
  3. public static void main(String[] args) {
  4. int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  5. int[] arr2 = new int[arr1.length];
  6.  
  7. printWithSeperatorUsingForLoop(',', arr1);
  8. printWithSeperatorUsingWhileLoop('-', arr2);
  9.  
  10. reverseArrayCopyWithForLoop(arr1, arr2);
  11. reverseArrayCopyWithWhileLoop(arr1, arr2);
  12.  
  13. printWithSeperatorUsingDoWhileLoop(',', arr1);
  14. printWithSeperatorUsingForLoop('-', arr2);
  15. }
  16.  
  17. private static void printWithSeperatorUsingForLoop(char seperator, int[] array) {
  18. for(int i = 0; i < array.length; i++) {
  19. System.out.print(array[i]);
  20.  
  21. if(i < array.length - 1) {
  22. System.out.print(seperator);
  23. } else {
  24. System.out.println();
  25. }
  26. }
  27. }
  28.  
  29. private static void printWithSeperatorUsingWhileLoop(char seperator, int[] array) {
  30. int i = 0;
  31.  
  32. while(i < array.length) {
  33. System.out.print(array[i]);
  34.  
  35. if(i < array.length - 1) {
  36. System.out.print(seperator);
  37. } else {
  38. System.out.println();
  39. }
  40.  
  41. i++;
  42. }
  43. }
  44.  
  45. private static void printWithSeperatorUsingDoWhileLoop(char seperator, int[] array) {
  46. int i = 0;
  47.  
  48. do {
  49. System.out.print(array[i]);
  50.  
  51. if(i < array.length - 1) {
  52. System.out.print(seperator);
  53. } else {
  54. System.out.println();
  55. }
  56.  
  57. i++;
  58. } while(i < array.length);
  59. }
  60.  
  61. private static void reverseArrayCopyWithForLoop(int[] source, int[] destination) {
  62. for(int i = source.length - 1, j = 0; i >= 0; i--, j++) {
  63. destination[j] = source[i];
  64. }
  65. }
  66.  
  67. private static void reverseArrayCopyWithWhileLoop(int[] source, int[] destination) {
  68. int i = source.length - 1, j = 0;
  69. while(i >= 0) {
  70. destination[j] = source[i];
  71. i--;
  72. j++;
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement