fork(1) download
  1. import java.util.Arrays;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. /*
  7.   System.out.println("Hello World!");
  8.   int[] a = {5, 4, 3, 2, 1};
  9.   mergeSort(a);
  10.   */
  11.  
  12. int[][] stuff = {
  13. {1, 2, 3},
  14. {4, 5, 6},
  15. {7, 8, 9}
  16. };
  17.  
  18. System.out.println("Initial state of array:");
  19. for (int row = 0; row < stuff.length; row++) {
  20. System.out.println(Arrays.toString(stuff[row]));
  21. }
  22. System.out.println(' ');
  23.  
  24.  
  25. flipHorizontal(stuff);
  26. System.out.println("Horizontally flipped:");
  27. for (int row = 0; row < stuff.length; row++) {
  28. System.out.println(Arrays.toString(stuff[row]));
  29. }
  30. System.out.println(' ');
  31.  
  32. flipVertical(stuff);
  33. System.out.println("Vertically flipped:");
  34. for (int row = 0; row < stuff.length; row++) {
  35. System.out.println(Arrays.toString(stuff[row]));
  36. }
  37.  
  38. }
  39.  
  40. // Swap left/right
  41. public static void flipHorizontal(int[][] arrayToFlip) {
  42. int columnIndexToStopAt = arrayToFlip[0].length / 2;
  43.  
  44. for (int currentRowIndex = 0; currentRowIndex < arrayToFlip.length; currentRowIndex++) {
  45. for (int currentColumnIndex = 0; currentColumnIndex < columnIndexToStopAt; currentColumnIndex++) {
  46.  
  47. int lastColumnIndex = arrayToFlip[0].length - 1;
  48. int oppositeColumnIndex = lastColumnIndex - currentColumnIndex;
  49.  
  50. int temp = arrayToFlip[currentRowIndex][currentColumnIndex];
  51.  
  52. arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[currentRowIndex][oppositeColumnIndex];
  53. arrayToFlip[currentRowIndex][oppositeColumnIndex] = temp;
  54.  
  55. }
  56. }
  57. }
  58.  
  59. // Swap top/bottom
  60. public static void flipVertical(int[][] arrayToFlip) {
  61. int rowIndexToStopAt = arrayToFlip.length / 2;
  62.  
  63. for (int currentRowIndex = 0; currentRowIndex < rowIndexToStopAt; currentRowIndex++) {
  64. for (int currentColumnIndex = 0; currentColumnIndex < arrayToFlip[0].length; currentColumnIndex++) {
  65.  
  66. int lastRowIndex = arrayToFlip.length - 1;
  67. int oppositeRowIndex = lastRowIndex - currentRowIndex;
  68.  
  69. int temp = arrayToFlip[currentRowIndex][currentColumnIndex];
  70.  
  71. arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[oppositeRowIndex][currentColumnIndex];
  72. arrayToFlip[oppositeRowIndex][currentColumnIndex] = temp;
  73.  
  74. }
  75. }
  76. }
  77.  
  78. }
  79.  
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Initial state of array:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
 
Horizontally flipped:
[3, 2, 1]
[6, 5, 4]
[9, 8, 7]
 
Vertically flipped:
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]