This is a example program that creates a 2d array (called matrix
):
public static void main(String[] args) throws Exception {
// will initialize the matrix with null references
String[][] matrix = new String[4][3];
// fill it with some values
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[0].length; j++)
matrix[i][j] = "Hello";
// set a specific matrix element
matrix[1][1] = "World";
// print it
for (int i = 0; i < matrix.length; i++)
System.out.println(Arrays.toString(matrix[i]));
}
It will print:
[Hello, Hello, Hello]
[Hello, World, Hello]
[Hello, Hello, Hello]