Java Two Dimensional Array
Chapter:
Miscellaneous
Last Updated:
28-10-2016 12:50:30 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaTwoDimensionalArray {
public static void main(String args[]) {
int row, col, i, j;
int arr[][] = new int[10][10];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Number of Row for Array (max 10) : ");
row = scanner.nextInt();
System.out.print("Enter Number of Column for Array (max 10) : ");
col = scanner.nextInt();
System.out.print("Enter " + (row * col) + " Array Elements : ");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
arr[i][j] = scanner.nextInt();
}
}
System.out.print("The Array is :\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
/* ............... END ............... */
Output
Enter Number of Row for Array (max 10) : 3
Enter Number of Column for Array (max 10) : 3
Enter 9 Array Elements : 3
43
4
5
6
7
7
8
8
The Array is :
3 43 4
5 6 7
7 8 8
Notes:
-
Two dimensional array can be made in Java Programming language by using the two loops, the first one is outer loop and the second one is inner loop. Outer loop is responsible for rows and the inner loop is responsible for columns. And both rows and columns combine to make two-dimensional (2D) Arrays.
Tags
Java Two Dimensional Array, Java, Miscellaneous