0% found this document useful (0 votes)
65 views

Two Dimensional Array in Java - JavaTutoring-por

The document discusses two dimensional arrays in Java. It defines a two dimensional array as an array with rows and columns where each row contains its own elements. It provides examples of initializing and accessing elements in a two dimensional array using standard methods, for loops, Scanner class input, and strings. Code samples are given to demonstrate declaring and printing the elements of two dimensional arrays using each approach.

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Two Dimensional Array in Java - JavaTutoring-por

The document discusses two dimensional arrays in Java. It defines a two dimensional array as an array with rows and columns where each row contains its own elements. It provides examples of initializing and accessing elements in a two dimensional array using standard methods, for loops, Scanner class input, and strings. Code samples are given to demonstrate declaring and printing the elements of two dimensional arrays using each approach.

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

7/29/2019 Two Dimensional Array In Java - JavaTutoring

BEGINNERS PROGRAMS : Java Program To Reverse An Array | Programs

HOME JAVA DATA TYPES JAVA VARIABLES CONTROL STATEMENTS JAVA ARGUMENTS

C TUTORIALS

Two Dimensional Array In Java – JavaTutoring


 in Java Programs  Comments Offon Two Dimensional Array In Java – JavaTutoring

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.

The means used in this piece are as follows:

Using Standard Method


Using For Loop
Using Scanner
Using String

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.

The elements entered as per the example are as follows:

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.

Two Dimensional – Using Standard Method


1 class TwodimensionalStandard
2 {
3 public static void main(String args[])
4 {
5 int[][] a={{10,20},{30,40}};//declaration and initialization
6 System.out.println("Two dimensional array elements are");
7 System.out.println(a[0][0]);
8 System.out.println(a[0][1]);
9 System.out.println(a[1][0]);
10 System.out.println(a[1][1]);
11 }
12 }

Output:

1 Two dimensional array elements are


2 10
3 20
4 30
5 40

Using For Loop


1. In two dimensional array represent as rows and columns.

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:

1 Two dimensional array elements are


2 10
3 20
4 30
5 40
6 50
7 60

Using Scanner

1. Read the row length, column length of an array using sc.nextInt() method of Scanner class.

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
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:

1 Enter Row length of an array :


2 2
3 Enter column length of an array :
4 3
5 Enter 6 Elements to Store in Array :
6 1
7 2
8 3
9 4
https://javatutoring.com/java-two-dimensional-array/ 5/8
7/29/2019 Two Dimensional Array In Java - JavaTutoring

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

Two Dimensional – Using String

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].

2) Here i indicates row number and j indicates column number

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:

1 Two dimensional string array elements are :


2
3 str[0][0]:one
4 str[0][1]:two
https://javatutoring.com/java-two-dimensional-array/ 6/8

You might also like