java - Effectively iterate over two dimensional array -
currently fiddling around 2 dimensional arrays learning purposes, , have arrived @ problem can't see solution to.
the goal build following output (see grid further down)
0.0: 0.1 1.0: 1.1 2.0: 2.1 3.0: 3.1 4.0: 4.1 0.0: 0.2 1.0: 1.2 2.0: 2.2 3.0: 3.2 4.0: 4.2 ect
i believe have achived following piece of code.
stringbuilder sb2 = new stringbuilder(); string newline = system.getproperty("line.separator"); //grid1[0].length = 7 (int col = 0; col < (grid1[0].length - 1); col++) { (int row = 0; row < grid1.length; row++) { sb2.append(grid1[row][0] + ": "); sb2.append(grid1[row][col+1] + newline); } sb2.append(system.getproperty("line.separator")); } system.out.println(sb2.tostring());
my question is, if it's possible achieve same goal running on array in reverse order (see below), , if more efficient way have arrived to.
for (int row = 0; row < grid1.length; row++) { (int col = 0; col < grid1[0].length; col++) { //something } }
yes can, can use :
for (int row = grid1.length - 1; row >= 0; row--) { (int col = grid1[0].length - 1; col >= 0; col--) { ... } }
edit
i tried , show me :
int grid1[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; (int row = grid1.length - 1; row >= 0; row--) { (int col = grid1[0].length - 1; col >= 0; col--) { system.out.print(grid1[row][col]+ " "); } system.out.println(); } 9 8 7 6 5 4 3 2 1
instead of :
1 2 3 4 5 6 7 8 9
Comments
Post a Comment