Java MultiDimensional Arrays

MultiDimensional Arrays are used to store the data in a tabular form. A multidimensional array is an array of arrays. For example, storing the employee id and name of a employee can be easily done using multidimensional arrays.

Example :-          int[][] a = new int[5][5];

Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 25 elements.

There are following step to create Multidimensional Arrays.

  • Create Scanner class to enter user input.
  • Declare the size of Multidimensional Arrays.
  • Use sc.nextInt() method to enter number of row and column.
  • Traverse the for loop at depend on number of row and column.
  • Enter the user input to create matrix and store data in a[i][j] parameter.
  • Now traverse the another two for loop to show the matrix.
				
					import java.util.Scanner;

public class MultidimensionalArray {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row, column;
		int a[][] = new int[5][5];
		System.out.println("enter the number of rows :- ");
		row = sc.nextInt();
		System.out.println("enter the number of columns :- ");
		column = sc.nextInt();
		System.out.println("enter the elements to create Matrix :-");
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		System.out.println("Now the Multidimensional matrix are :- ");
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}

	}

}
				
			

Output :-
enter the number of rows :-
3
enter the number of columns :-
4
enter the elements to create Matrix :-
12
23
34
56
76
43
78
98
34
30
74
89
Now the Multidimensional matrix are :-
12 23 34 56
76 43 78 98
34 30 74 89