java display two dimensional array

https:/‮‬/www.lautturi.com
java display two dimensional array

To display the elements of a two-dimensional array in Java, you can use a loop to iterate through the rows and columns of the array and print the elements to the console.

Here's an example of how to do this using a nested for loop:

int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length; j++) {
        System.out.print(array[i][j] + " ");
    }
    System.out.println();
}

This will output the following:

1 2 3 
4 5 6 
7 8 9

You can also use a for-each loop to iterate through the rows of the array and a standard for loop to iterate through the columns:

int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

for (int[] row : array) {
    for (int element : row) {
        System.out.print(element + " ");
    }
    System.out.println();
}

This will also output the elements of the two-dimensional array in a grid format.

You can use a similar approach to display the elements of a two-dimensional array of any size.

Created Time:2017-11-03 15:57:11  Author:lautturi