2 Dimensional Arrays in Java

In this chapter we are going to learn about 2D arrays. In our previous lesion we learnt about 1D array. Let’s try to understand the importance of learning 2 dimensional arrays.



In advanced and dynamic programming there are many uses of 2D arrays and it becomes very helpful to make complex programs. That is why you should learn 2D arrays very carefully.

In this lesion, we are going to be learning 2D arrays from a very basic stage for example, how to declare a 2 dimensional array, the various uses of 2D array, visualizing array inside the memory etc.

We will also learn how to take user input, how to print on the screen etc. We will be learning all these concepts with coding examples so it becomes easier for you to understand and you learn the concept quickly.

 

What is 2 Dimensional Arrays in Java Programming?

In mathematics, you must have learnt matrix in 11th or 12th Standard. If you remember in matrix there were some rows and columns. Rows and columns both were having numbering (1,2,3.. and so on).

We were using these matrixes to write some number on those boxes. We could also find the location of any particular number on the matrix with the help of row and column numbering. The combination of numbers of a row and column is the location of any data plotted on the matrix table.

Those who are not from mathematics background, you can also understand the concept easily with the help of below diagram.

 

 

0

1

2

3

0

44

56

54

76

1

89

33

24

65

2

29

34

76

19

3

21

43

29

58

 

In the above diagram, there are 4 rows, 4 columns & 16 cells. Row’s and column’s numbering stars from 0. In each cell, there are some numbers written. Now if you want to ascertain the location of any plotted number on the matrix, you can do the same by row and column numbering.

For example, in matrix, the location of number “19” is written as (2,3), because the number “19” is plotted on the row number 2 & on the column number 3. This is the concept of matrix and it is very simple.

Now, if we put the same math’s structure to coding and through programming, the same matrix structure will be known as 2D arrays in Java. So, this type of rectangular structure having rows, columns, numbering, data plotting are known as 2D array in Java programming.

The data plotted are simply data stored in the memory in liner fashion. Since Java is a “0” index based language, the indexing of 2D arrays will start from “0”.

The size of the each cell of the array is defined by the size of the variable type which is stored in the array. For example if an integer variable is store in each cell of the array, the each cell of the array will be of 4 bytes. Similar if it is a Boolean array  the size of the cell will be 1 byte.

We can also ascertain the total memory consumption of the complete 2D array by simply multiplying the number of rows by number of columns and size of each cell in the array.

 

Declaration of 2 Dimensional Arrays:

          

type [][] arrayName = new type [rows][columns];

 

First we have to specify the data type of our array. Unlike 1D arrays there are 2 square brackets in 2D arrays since the array is in two dimensions. We have to specify the numbers of columns and rows both while declaring a 2D array. In 1 dimensional array, we just had to specify the number of columns and that’s it, rows were fixed in 1 dimensional array (i.e only 1).

 

Rest of the syntax in Java 2D array is similar as Java 1D array. The only difference in 2D array is, only 1 square bracket is there in Java 1D array.

 

Going back to the tabular example, if we have to access number “19” in 2D array, we have to access the same by arrayName[2][3], where number 2 denotes the row number and number 3 denotes the column number. That is how we can access the location in any 2D array.

 

Coding Example of Java 2D arrays:

In the below example we have declared a 2D array in Java practical programming. At the same time we have also written code in order to get user input of data and later we have printed those data or values on the screen stored in the memory.

 

import java.util.*;

 

public class JavaArray {

 

     public static void main(String[] args) {

         

          Scanner sc = new Scanner(System.in);

         

          System.out.println("Enter Numbe of Rows");

          int rows = sc.nextInt();

          System.out.println("Enter Numbe of Columns");

          int cols = sc.nextInt();

         

          int [][] numbers = new int [rows][cols];

         

          for(int i=0;i<rows;i++) {

              for (int j=0;j<cols;j++) {

                  

                   System.out.println("Enter the value in row no."+(i+1)+" & column no."+(j+1));

                   numbers[i][j] = sc.nextInt();

                  

              }

          }

         

          for(int i=0;i<rows;i++) {

              for (int j=0;j<cols;j++) {

 

                   System.out.println("Value stored in row no."+(i+1)+" & column no."+(j+1)+" is "+numbers[i][j]);

         

                  

              }

          }

 

     }

 

}

 

In 1D array we used only single loop to take input or print the values of the array on the screen. In 1D array we had to access only one row and inside the row there were spaces indexed 0 onward.

 

But in 2D array there are multiple rows and each row is having cells starting the index value from 0. So there are more values to be accessed while we deal with 2 dimensional arrays compared to 1D array.

 

In above example we used nested loop to access the memory of the 2 dimensional arrays. Without nested loop we cannot access 2D arrays. Now you must be thinking what is nested loop?

 

When there is a loop inside another loop, it is known as nested loop. The outer loop accesses through the rows while the inter loop accesses through the columns. This is how we can loop through a 2D array.

 

Conclusion:

We hope the concept for 2D arrays have been clear to you.  In upcoming lesions we will be learning more interesting concepts. Thank you for visiting our website.

 

 

2 Dimensional Arrays in Java 2 Dimensional Arrays in Java Reviewed by Technobits on November 09, 2022 Rating: 5

No comments:

Powered by Blogger.