Skip to main content

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.

 

 

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...