Two Dimensional Array in Java - JavaTutoring
Two Dimensional Array in Java - JavaTutoring
Similarly, a two-dimensional array is an array which technically has one row of elements, however, each
BEGINNERS PROGRAMS : Insertion Sort Java Algorithm – 2 Ways | Java Sortings
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.
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 Thus, there will be a total of 6 elements that can go into a 2×3 Matrix.
used to store information of one particular type of variable.
The elements entered as per the example are as follows:
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.
1
3 1 class TwodimensionalLoop
2 {
4
3 public static void main(String args[])
5 4 {
5 int[][] a={{10,20},{30,40},{50,60}};//declaration and initialization
6
6 System.out.println("Two dimensional array elements are");
Hence, the elements are arranged accordingly and you will get your two-dimensional array. 7 for (int i = 0; i < 3; i++)
8 {
9 for (int j = 0; j < 2; j++)
Two Dimensional – Using Standard Method 10 {
11 System.out.println(a[i][j]);
1 class TwodimensionalStandard
12 }
2 {
13 }
3 public static void main(String args[])
14 }
4 {
15 }
5 int[][] a={{10,20},{30,40}};//declaration and initialization
6 System.out.println("Two dimensional array elements are"); Output:
7 System.out.println(a[0][0]);
8 System.out.println(a[0][1]); 1 Two dimensional array elements are
9 System.out.println(a[1][0]); 2 10
10 System.out.println(a[1][1]); 3 20
11 } 4 30
12 } 5 40
6 50
Output:
7 60
4 30
5 40 2) Declare the array with the dimension row, column.
3) for i=0 to i<row for j=0 to j<column sc.nextInt() reads the entered number and insert the element at
Using For Loop a[i][j].
5 { 10 5
6 11 6
7 Scanner sc=new Scanner(System.in); 12 Elements in Array are :
8 System.out.println("Enter Row length of an array : "); 13 Row [0]: Column [0] :1
9 int row=sc.nextInt(); 14 Row [0]: Column [1] :2
10 System.out.println("Enter column length of an array : "); 15 Row [0]: Column [2] :3
11 int column=sc.nextInt(); 16 Row [1]: Column [0] :4
12 int a[][]=new int[row][column];//declaration 17 Row [1]: Column [1] :5
13 System.out.print("Enter " + row*column + " Elements to Store in Array :\n 18 Row [1]: Column [2] :6
14 for (int i = 0; i < row; i++)
15 { Two Dimensional – Using String
16 for(int j = 0; j < column; j++) 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
17 { element which is at the index str[i][j].
18 a[i][j] = sc.nextInt();
19 } 2) Here i indicates row number and j indicates column number
20 }
21 System.out.print("Elements in Array are :\n"); 1 class TwoDimensionalString
22 for (int i = 0; i < row; i++) 2 {
23 { 3 public static void main(String[] args)
24 for(int j = 0; j < column; j++) 4 {
25 { 5 String[][] str = new String[][]{{"one", "two"}, {"three", "four"},{"f
26 System.out.println("Row ["+i+"]: Column ["+j+"] :"+a[i][j]); 6 System.out.println("Two dimensional string array elements are :\n");
27 } 7 for (int i = 0; i < 3; i++)
28 } 8 {
29 } 9 for (int j = 0; j < 2; j++)
30 } 10 {
11 System.out.println("str["+i+"]["+j+"]:"+str[i][j]);
Output:
12 }
13 }
1 Enter Row length of an array : 14 }
2 2 15 }
3 Enter column length of an array :
4 3 Output:
5 Enter 6 Elements to Store in Array :
6 1 1 Two dimensional string array elements are :
7 2 2
8 3 3 str[0][0]:one
9 4 4 str[0][1]:two
https://javatutoring.com/java-two-dimensional-array/ 5/8 https://javatutoring.com/java-two-dimensional-array/ 6/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring 7/29/2019 Two Dimensional Array In Java - JavaTutoring
5 str[1][0]:three
6 str[1][1]:four C Program To Compare Two Strings – 3 Easy Java Program Calculate Perimeter Of Square |
Ways | C Programs Programs
7 str[2][0]:five
8 str[2][1]:six
C Program Find Maximum Between Two Java Program To Calculate Area Of Triangle –
Numbers | C Programs 5 Ways
Tweet Like 0 Share
C Program Number Of Alphabets, Digits & Square Star Pattern Program In Java –
Special Character In String | Programs Patterns
« Previous:
Multilevel Inheritance In Java – Tutorial
& Examples
Next:
One Dimensional Array In Java –
Tutorial & Example » C Program Mirrored Half Diamond Star Pattern
| C Patterns
Java Program Convert Decimal To Hexadecimal
| Vice Versa
RELATED POSTS ! C Program To Copy All Elements From An Java Hollow Inverted Mirrored Right Triangle
Array | C Programs Star Pattern
© 2019. Copyrighted Protected. Duplication or Copying Our Site Content Is Strictly Prohibited. However, Refer
One Dimensional Array In Multi Dimensional Array In Java Program To Check Even
Java – Tutorial & Example Java – Tutorial & Program Numbers | 4 Ways