Advertisement
Guest User

Untitled

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