Consider the following definitions
int[ ][ ] numbers = {{1, 2, 3},
{4, 5, 6} };
The following code below produces
for (int row = numbers.length - 1; row >= 0; row -- )
{
for (int col = 0; col < numbers[0].length; col ++)
{
System.out.print (numbers [row][col]);
}
}
The code produces the output of 456123
Initially we start row from 1 and col runs from 0 to 2.
which prints numbers[1][0] numbers[1][1] numbers[1][2] which are 456 respectively
then row becomes 0 and col runs from 0 to 2 printing
numbers[0][0] numbers[0][1] numbers[0][2] which are 123 respectively
thus output is 456123
Note no space or new line is added as system.out.print has been used
Get Answers For Free
Most questions answered within 1 hours.