Example 1: multidimensional array

In this code example, a multidimensional array is output with 2 nested for-loops.

import java.util.Arrays;

public class MultidimensionalArrayExample {
	public static void main(String[] args) {
		int[][] myArray = new int[2][4];
 
		myArray[0][0] = 1;
		myArray[0][1] = 2;
		myArray[0][2] = 3;
		myArray[0][3] = 4;
		myArray[1][0] = 5;
		myArray[1][1] = 6;
		myArray[1][2] = 7;
		myArray[1][3] = 8;

		for (int i = 0; i < myArray.length; i++) {
			for (int j = 0; j < myArray[i].length; j++) {
				System.out.print(myArray[i][j]);
			}
			System.out.println();
		}
	}
}
Output
1234
5678