I am teaching myself Java for about 6 months so I am pretty new to coding and I need your help here. I want to completely reverse a 2 dimensional array (a) using 2 loops and writing it in another array but I get an OutOfBondsError as I tried to change my line and column values. It seems like I don't fully understand how these nested loops work...
starting array a:
1.) 1.0 2.0 3.0 2.0
2.) 0.0 1.0 2.0 0.0
3.) 0.0 0.0 1.0 3.0
My code should reverse a like this.
Target array t:
1.) 3.0 1.0 0.0 0.0
2.) 0.0 2.0 1.0 0.0
3.) 2.0 3.0 2.0 1.0
final double[][] a = new double[3][];
a[0] = new double[]{1, 2, 3, 2}; // I
a[1] = new double[]{0, 1, 2, 0}; // II
a[2] = new double[]{0, 0, 1, 3}; // III
int numLines = a.length;
int numCols = a[0].length;
double[][] t = new double[a.length][a[0].length];
for (int currentColumn = numCols - 2; currentColumn >= 0; currentColumn--) {
for (int currentLine = numLines - 1; currentLine >= 0; currentLine--) {
t[currentLine][currentColumn] = a[currentColumn][currentLine];
}
System.out.println();
}
ArrayTools.printMatrix(t);
The console output from my printMatrix method for array t looks like this:
1.) 1.0 0.0 0.0 0.0
2.) 2.0 1.0 0.0 0.0
3.) 3.0 2.0 1.0 0.0
It's pretty close to what I want but I dont know how to get the last colum from a written into t.
I would be happy if you guys could explain me what happens here and how to slove this problem!