2D Array in JAVA

2D arrays in JAVA

In this part of tutorial you will learn 1) What is a 2D array? 2) Why we use 2D arrays? 3) How we use 2D arrays in java programming? What is a 2D array? One dimensional array is a linear array, while the two dimensional array is such an array which has the rows as well as columns. The 2 dimensional is an array within an array so the each entry is divided further allowing us more entries to be included. It is a nonlinear array which is has a vertical length and horizontal length as well. Why we use 2D arrays? To visualize certain real life things in programming terms two dimensional arrays are used. The chess game and digital image can be represented using the two dimensional arrays. The image has some value at each of its pixel, similarly the chess game has values present at each point. One dimensional and two dimensional arrays are just the same but they only differ in their representation as if we want to place the course being taught in a list form or in the form of table, its upon our choice, so we may use the 2D array or 1D array. The two dimensional array can be thought of a matrix for our ease of understanding. How we use the 2D arrays in JAVA? The two dimensional arrays have the properties same as that of the one dimensional arrays. The insertion and deletion can be done at any point. The syntax for declaring and initializing an array is same in both. The syntax is
  1. String[][] my2Darray = {{ "I",  "love" },
  2.                                 { "JAVA", "it" },
  3.                                 { "is",  "best"  }};
To access an element of the array we use the same method as of the one dimensional array but the rows as well as columns have to be mentioned. The first entry represent the row and the second entry represent the columns. We can also use a for loop to retrieve the elements of a 2D array. As shown below
  1. for (int i=0; i< col ; i++)
  2. for (int j = 0; j < rows; j++) {
  3.                            System.out.print(my2Darray[i][j]);
  4.                            System.out.println();
  5.                           }
Deletion We can also delete the entries of an array by assigning them zero value. As for the above array the data type of the 2D array is string so we set a value equal to null for each element using the for loop.
  1. for (int i = 0; i <3; i++) {
  2.                         System.out.println();
  3.                            for (int j = 0; j < 2; j++) {
  4.                           my2Darray[i][j]=null;
  5.                          
  6.                           }
  7.                 }
Code Example: This program has been written to clear your concepts about the 2D arrays in java.
  1. package test;
  2. import java.util.*;
  3. public class myclass {
  4.  
  5.         public static void main(String[] args) {
  6.                 String[][] my2Darray = {{ "I",  "love" },
  7.                                 { "JAVA.", "it" },
  8.                                 { "is",  "best"  }};
  9.  
  10.                          
  11.                 System.out.print("Single element retrieve result:");
  12.         System.out.print(my2Darray[0][1]); // element wise data extraction
  13.         System.out.println();
  14.         System.out.print("Showing all elements of 2D array");
  15.         System.out.println();
  16.        
  17.                 for (int i = 0; i <3; i++) {   //used to extract whole contact of 2D array
  18.                         System.out.println();
  19.                            for (int j = 0; j < 2; j++) {
  20.                            System.out.print(my2Darray[i][j]);
  21.                            System.out.println();
  22.                           }
  23.                         }
  24.                 //the code below is for deletion of all the elements of the array
  25.                 /*for (int i = 0; i <3; i++) {
  26.                         System.out.println();
  27.                            for (int j = 0; j < 2; j++) {
  28.                           my2Darray[i][j]=null;
  29.                           System.out.println();
  30.                           }
  31.                 } */
  32.         }
  33.  
  34. }
ScreenShot Screen Shot

Add new comment