2d Array Java

Hey guys!!! Welcome to flower brackets blog. In this post we are going to learn 2d array java.

2d Array Java

2d Array Java

Two dimensional array in java is an array of arrays. It is the simplest form of multi-dimensional array.

Also Read – Array Java

In java two dimensional array, data is stored in row and columns. Data is accessed using row and column index.

2d array java

Declare 2d array java

Now let’s see how to declare 2d array in java,

datatype[][] arrayName;

datatype - defines datatype of elements. For example int, float

arrayName - defines name of array. For example employee, students 

Likewise we can declare different data type of 2d array as shown below,

int[][] studentMarks; // declaring 2d array of Integer data type

byte[][] num; // declaring 2d array of Byte data type

short[][] number; // declaring 2d array of Short data type

long[][] twoDimensional; // declaring 2d array of Long datatype

float[][] floatNumber; // declaring 2d array of Float data type

double[][] doubleNumber; // declaring 2d array of Double data type

boolean[][] bool; // declaring 2d array of Boolean data type

char[][] ch; // declaring 2d array of Char data type

String[][] strArray; // declaring 2d array of String data type

Create 2d array java

To create two dimensional array in java “new” operator is used. Here’s how to create 2d array in java,

DataType arrayName[ ][ ] = new int[rowsize][columnsize];

Here’s how to declare and create a 2d array in java,

double employeeDetails[][];    // declaring 2d array in java

employeeDetails = new int[6][3]; // creating 2d array in java

In the above 2d array declaration we have used double data type. employeeDetails array accepts only (not other data types) double data type values.

Also Read – Learn Java Array

The row size of employeeDetails array is 6 and it accepts only 6 double values. If there is more than 6 values then it will throw an error.

If we store less than 6 values then remaining values will be initialized to default value, which is, zero.

Also Read – Two Dimensional Array Java Program

Next comes the column size which is 3. employeeDetails can store only 3 double values as columns.

As in row size, if we store more than 3 values then it will throw an error and if we store less than 3 values then remaining values will be initialized to default value, which is, zero.

Java initialize two dimensional array

Now let us learn how to initialize a 2d array in java. In java we can initialize 2d array in many ways,

Way 1 :

In this approach we will be not mentioning column size. Since compiler is smart enough to determine the size of the column by checking number of elements.

int[ ][ ] empSalary = new int[3][ ];

Way 2 :

Declaring and creating two dimensional array in java,

int[][] empSalary = new int[3][5]; // initialize two dimensional array java

empSalary[0][0] = 14K$; // Initializing Array elements at position [0][0]
empSalary[0][1] = 32K$; // Initializing Array elements at position [0][1]
empSalary[0][2] = 50K$; // Initializing Array elements at position [0][2]

Way 3 :

int[][] empID = {{11, 22, 33}, {14, 24, 34}, {23, 43, 63}, {35, 55, 75}};

Way 4 :

In order to store more than 100 (column) values in an array we can use nested for loop,

public class Java2DArray 
{
   public static void main(String[] args) 
   {
      int row, column;
      int empDetails[][] = new int[50][25];

      for(row = 0; row < 50 ; row++)
      {
         for(column = 0; column < 25; column++) 
         {
            empDetails[row][column] = row + column;
         }
      }
   }
}

Way 5 :

Here we have declared 2d array in java of size 4 rows X 3 columns and we have assigned values for one row.

Also Read – Java Find Max In Array

Remaining values will be initialized to default value, which is, zero.

int empDetails[][] = new int[4][3];
empDetails[0][0] = 14;
empDetails[0][1] = 32;
empDetails[0][2] = 50;

Access java 2d array elements

To access java 2d array elements we can use index. In java 2d array “index” means index position of two dimensional in java.

2d Array Java

Utilizing index position of two dimensional array in java we can change individual elements. For example,

public class TwoDArray 
{
   public static void main(String[] args) 
   {
      int empArray[][] = {{11, 22, 33}, {14, 24, 34}, {23, 43, 63}};

      System.out.println("Element at empArray[0][0] = " + empArray[0][0]);
      System.out.println("Element at empArray[0][1] = " + empArray[0][1]);
      System.out.println("Element at empArray[0][2] = " + empArray[0][2]);
      System.out.println("Element at empArray[1][0] = " + empArray[1][0]);
      System.out.println("Element at empArray[1][1] = " + empArray[1][1]);
      System.out.println("Element at empArray[1][2] = " + empArray[1][2]);
      System.out.println("Element at empArray[2][0] = " + empArray[2][0]);
      System.out.println("Element at empArray[2][1] = " + empArray[2][1]);
      System.out.println("Element at empArray[2][2] = " + empArray[2][2]);
   }
}

Output :

Element at empArray[0][0] = 11
Element at empArray[0][1] = 22
Element at empArray[0][2] = 33
Element at empArray[1][0] = 14
Element at empArray[1][1] = 24
Element at empArray[1][2] = 34
Element at empArray[2][0] = 23
Element at empArray[2][1] = 43
Element at empArray[2][2] = 63


Two dimensional array java example

Here we are going to learn two dimensional array example using method and without using method with an example.

Using method

public class Array2DJava 
{
   public static void main(String[] args) 
   {
      int employeeOne[][] = {{2,4,6,8},{10,12,14,16}};
      int employeeTwo[][] = {{1,3,5,7},{9,11,13,15}};

      System.out.println("Two dimensional array java : First Array ");
      displayArray(employeeOne);

      System.out.println("Two dimensional array java : Second Array ");
      displayArray(employeeTwo);
   }

   public static void displayArray(int a[][])
   {
      for(int row = 0; row < a.length; row++)
      {
         for(int column = 0; column < a[row].length; column++)
         {
            System.out.format("%d \t", a[row][column]);
         }
         System.out.println();
      }
   }
}

Output :

Two dimensional array java : First Array
2    4   6   8
10 12 14 16
Two dimensional array java : Second Array
1  3   5   7
9 11 13 15


Without using method

In 2d array java example we are going to declare and initialize values and print them. Here’s the two dimensional array example in java,

public class TwoDimensionalExample 
{
   public static void main(String[] args) 
   {
      int samMarks[][] = {{24, 25, 32}, {50, 60, 56}};
      int joeMarks[][] = {{23, 24, 41}, {53, 56, 80}};
      int Total[][] = new int[2][3];
      int row, column;

      for(row = 0; row < samMarks.length; row++)
      {
         for(column = 0; column < samMarks[0].length; column++) 
         {
            Total[row][column] = samMarks[row][column] + joeMarks[row][column]; 
         } 
      }

      System.out.println("Print 2d array java - Total : ");
      for(row = 0; row < samMarks.length; row++) 
      {
         for(column = 0; column < samMarks[0].length; column++) 
         {
            System.out.format("%d \t", Total[row][column]);
         }
         System.out.println("");
      }
   }
}

Output :

Print 2d array java – Total :
47 49 73
103 116 136


conclusion

So this is all about 2d array in java. I hope you guys have understood the concept of java 2d array.

You can subscribe to my blog flower brackets if you haven’t already. Do share this post if you like.

You May Also Like