Two Dimensional Array in Java - JavaTutoring-por
Two Dimensional Array in Java - JavaTutoring-por
HOME JAVA DATA TYPES JAVA VARIABLES CONTROL STATEMENTS JAVA ARGUMENTS
C TUTORIALS
Two Dimensional Array in Java Programming – In this article, we will explain all the various methods
used to explain the two-dimensional array in Java programming with sample program & Suitable examples.
All the methods will be explained with sample programs and suitable examples. The compiler has also been
added so that you understand the whole thing clearly.
An array, as we all know, is a collection of multiple elements of the same data type. Arrays are normally
used to store information of one particular type of variable.
A two-dimensional entity, in general, is something that has two specific parameters. Those two parameters
are usually length and breadth since it is a physical quantity.
https://javatutoring.com/java-two-dimensional-array/ 1/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring
Similarly, a two-dimensional array is an array which technically has one row of elements, however, each
row has a bunch of elements defined by itself.
Basically, you need to define both the rows and columns and then go ahead with declaring the elements in
the respective locations or indexes.
As you can see in the example given above, firstly, you need to specify the number of rows that you are
assigning with the array.
In this case, it is 2.
Next, you need to mention the number of columns that you want to assign with the array.
Here, it is 3.
Thus, there will be a total of 6 elements that can go into a 2×3 Matrix.
https://javatutoring.com/java-two-dimensional-array/ 2/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring
5
6
Hence, the elements are arranged accordingly and you will get your two-dimensional array.
Output:
2) To print the two-dimensional array, for loop iterates from o to i<3 for loop iterates from j=0 to j<2 print
the element which is at the index a[i][j].
https://javatutoring.com/java-two-dimensional-array/ 3/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring
1 class TwodimensionalLoop
2 {
3 public static void main(String args[])
4 {
5 int[][] a={{10,20},{30,40},{50,60}};//declaration and initialization
6 System.out.println("Two dimensional array elements are");
7 for (int i = 0; i < 3; i++)
8 {
9 for (int j = 0; j < 2; j++)
10 {
11 System.out.println(a[i][j]);
12 }
13 }
14 }
15 }
Output:
Using Scanner
1. Read the row length, column length of an array using sc.nextInt() method of Scanner class.
3) for i=0 to i<row for j=0 to j<column sc.nextInt() reads the entered number and insert the element at
a[i][j].
1 import java.util.*;
2 class TwoDimensionalScanner
3 {
4 public static void main(String args[])
https://javatutoring.com/java-two-dimensional-array/ 4/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring
5 {
6
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter Row length of an array : ");
9 int row=sc.nextInt();
10 System.out.println("Enter column length of an array : ");
11 int column=sc.nextInt();
12 int a[][]=new int[row][column];//declaration
13 System.out.print("Enter " + row*column + " Elements to Store in Array :\n
14 for (int i = 0; i < row; i++)
15 {
16 for(int j = 0; j < column; j++)
17 {
18 a[i][j] = sc.nextInt();
19 }
20 }
21 System.out.print("Elements in Array are :\n");
22 for (int i = 0; i < row; i++)
23 {
24 for(int j = 0; j < column; j++)
25 {
26 System.out.println("Row ["+i+"]: Column ["+j+"] :"+a[i][j]);
27 }
28 }
29 }
30 }
Output:
10 5
11 6
12 Elements in Array are :
13 Row [0]: Column [0] :1
14 Row [0]: Column [1] :2
15 Row [0]: Column [2] :3
16 Row [1]: Column [0] :4
17 Row [1]: Column [1] :5
18 Row [1]: Column [2] :6
1. To print the elements of two-dimensional string array for i=0 to i<3 for j=0 to j<2 prints the string
element which is at the index str[i][j].
1 class TwoDimensionalString
2 {
3 public static void main(String[] args)
4 {
5 String[][] str = new String[][]{{"one", "two"}, {"three", "four"},{"f
6 System.out.println("Two dimensional string array elements are :\n");
7 for (int i = 0; i < 3; i++)
8 {
9 for (int j = 0; j < 2; j++)
10 {
11 System.out.println("str["+i+"]["+j+"]:"+str[i][j]);
12 }
13 }
14 }
15 }
Output: