PJF16

swapArray TwoDimensions

Dec 9th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1.     public static int[] reverseOrder(int[] array) {
  2.  
  3.         return swap(array, array.length, 0);
  4.     }
  5.  
  6.     public static int[] swap(int[] array, int length, int pos) {
  7.  
  8.         if (pos * 2 <= length) {
  9.             int help;
  10.  
  11.             help = array[pos];
  12.             array[pos] = array[length - 1 - pos];
  13.             array[length - 1 - pos] = help;
  14.  
  15.             swap(array, length, pos + 1);
  16.         }
  17.  
  18.         return array;
  19.     }
  20.  
  21.     public static int[][] myreverseOrder(int[][] array) {
  22.  
  23.         return reverseIt(array, false, 0);
  24.     }
  25.  
  26.     public static int[][] reverseIt(int[][] array, boolean isReversed, int pos) {
  27.         if (pos * 2 <= array.length + 1) {
  28.             if (isReversed) {
  29.                 array[pos] = reverseOrder(array[pos]);
  30.                 return reverseIt(array, true, pos + 1);
  31.             } else {
  32.                 Collections.reverse(Arrays.asList(array));
  33.                 return reverseIt(array, true, pos);
  34.             }
  35.         } else {
  36.             return array;
  37.         }
  38.  
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment