For an array of arrays that is two dimensional, reverse all the elements. That is our task here today. There are many approaches of going about this in Java, but here is my approach:
public class Main {
public static void main(String[] args) {
arraysProgram1();
}
public static void arraysProgram1() {
// array of arrays with two dimensions
int[][] values = {
{ 4, 6, 3, 2, 8, -3, 90, 333 },
{ 40, 23, 45, 34, 23, 23 },
{ 0 }
};
// function that prints the values
// of the array of array (defined in the class
// called `MultiDimensional` that I have made here)
Helper.printValues(values);
System.out.println("Reversed values");
// make the array of arrays equal
// to the result of reverseValues() function
values = Helper.reverseValues(values);
Helper.printValues(values);
}
}
Helper class with the function definitions for printValues(…) and reverseValues(…).
public class Helper {
public static void printValues(int[][] values) {
// values will be printed with
// commas in between them in a nice box
// e.g:
// [
// [ 10, 3, -5 ],
// [ 9, -9, 21 ],
// [ 0 ]
// ]
// which may appear reminicent of how Python prints arrays
System.out.print("[\n");
// for the length of the first (outer) dimension of the array
for(int i = 0 ; i < values.length; i++) {
System.out.print(" [");
// traverse inner arrays (which are themselves elements)
// for each element of the
// outer array
for (int j = 0; j < values[i].length; j++) {
// if the printed value is less
// than the penultimate element in its array,
// print a comma, otherwise, do append an empty string.
System.out.print(
values[i][j] +
(j < values[i].length - 1 ? ", ": "")
);
}
// print a closing bracket to
// show that the array has been traversed
// then, if there is at least
// one more array that follows, print a comma
// and make a new line. otherwise, add an empty string.
System.out.print("]" + (i < values.length -1 ? ",\n":"" ));
}
// end two dimensional
// array of arrays formatting
System.out.println("\n]");
}
/**
* Reverses a two dimensional array of arrays
* @param values
* @return
*/
public static int[][] reverseValues(int[][] values) {
// create new two dimensional array of arrays
// with a matching outer
// size of the original
int[][] new_values
= new int[values.length][];
// for each inner array
// inside the outer array, iterate
for(int i = 0; i < values.length; i++) {
// create inner array for the new,
// output array (new_values), and
// make its size equal to the length
// of the currently selected inner array
// minus the index minus one
int[] inner = values[values.length - i - 1];
// add inner array to the output array
new_values[i] = new int[inner.length];
// for each element of the inner array, iterate
for(int j = 0; j < inner.length; j++) {
// populate inner array values
// with values of inner array but reversed
new_values[i][j] = inner[inner.length - j - 1];
}
}
return new_values;
}
}
Output:
[
[4, 6, 3, 2, 8, -3, 90, 333],
[40, 23, 45, 34, 23, 23],
[0]
]
Reversed values
[
[0],
[23, 23, 34, 45, 23, 40],
[333, 90, -3, 8, 2, 3, 6, 4]
]