public class multiarraysExample1
{
public static void main(String[] args)
{
int[][] magicSquare =
{
{16,3,2,13},
{5,10,11,8},
{9,6,7,12 },
{4,15,14,1}
};
for (int i = 0; i < magicSquare[0].length; i++)
{
System.out.printf("%d
", magicSquare[0][i]);
}
for (int i = 0; i< magicSquare.length; i++)
{
for (int j = 0; j< magicSquare[i].length; j++)
{
System.out.printf("%d
", magicSquare[i][j]);
}
}
for(int[] row: magicSquare)// loop through the rows first
{
for(int value : row) // loop through the elements of the row
System.out.printf("%d
", value);
}
}
}
To print out a quick-and-dirty list of the elements of a two-dimensional array, call:
System.out.println(Arrays.deepToString(magicSquare)); // print out a quick-and-dirty