Two dimensional array java program

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn two dimensional array java program.

two dimensional array java program

What is two dimensional array in java?

Two dimensional arrays are just array of an array. It is like container that can hold specific values.

Also read – java array

// declaring two dimensional integer array
int numbers[][] = new int[4][5];

"numbers" array can hold maximum of 20 elements of type int.

Two Dimensional Array Java Program

Here’s an example of primitive type,

public class TwoDimensionalArrayDemo
{
   public static void main(String[] args)
   {
      int numbers[][] = new int[4][5];
      for(int a = 0; a < numbers.length; a++) 
      {
         for(int b = 0; b < numbers[a].length; b++) 
         {
            numbers[a][b] = b;
            System.out.print(numbers[a][b] + " ");
         }
         System.out.println("");
      }
   }
}

Output:

0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4


Here’s java program of two dimensional array in java using scanner,

import java.util.Scanner;

public class TwoDimensionalDemo
{
   public static void main(String[] args)
   {
      int a, b, row, column;
      int arrEven[][] = new int[10][10];
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of rows (Max. limit 10) :  ");
      row = sc.nextInt();
      System.out.print("Please enter number of columns (Max. limit 10) : ");
      column = sc.nextInt();
      System.out.print("Please enter " + (row * column) + " array elements : ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            arrEven[a][b] = sc.nextInt();
         }
      }

      System.out.print("The two dimensional array is : \n");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            System.out.print(arrEven[a][b]+ "  ");
         }
         System.out.println();
      }
      sc.close();
   }
}

Output:

Please enter number of rows (Max. limit 10) :  3
Please enter number of columns (Max. limit 10) : 3
Please enter 9 array elements :
2 4 6 8 10 12 14 16 18
The two dimensional array is :
2 4 6
8 10 12
14 16 18


What is multi dimensional array in java?

An array which has non symmetric sizes or more than one size is called multidimensional array. For example,

Also read – how to create matrix in java

int evenArr[][] = { {2, 4, 6},{8, 10, 12} };
int oddArr[][] = { {1, 3, 5, 7},{15},{9, 11} };

public class MultidimensionalArrayDemo 
{
   public static void main(String[] args) 
   {
      int evenArr[][] = {{2,4,6},{8,10,12}};
      int oddArr[][] = {{1,3,5,7},{15},{9,11}};
      System.out.println("First array, that is, evenArr : ");
      print(evenArr);
      System.out.println("Second array, that is, oddArr : ");
      print(oddArr);
   }
	
   public static void print(int arr[][])
   {
      // looping through rows
      for(int row = 0; row < arr.length; row++)
      {
         // looping though columns
         for(int col = 0; col < arr[row].length; col++)
         {
            System.out.print(arr[row][col] + "\t");
         }
         System.out.println();
      }
   }
}

Output:

First array, that is, evenArr :
2 4 6
8 10 12
Second array, that is, oddArr :
1 3 5 7
15
9 11


What is a three dimensional array?

Three dimensional array is an array of two dimensional arrays. Like two dimensional arrays, three dimensional array can vary in length.

Here’s how we can initialize a 3d array in java,

int num[][][] = { 
                   {
                      {1, -2, 3},
                      {2, 3, 4}
                   },
                   {
                      {-4, -5, 6, 9},
                      {1},
                      {2, 3}
                   }
                 };
OR

int num[][][] = new int[3][2][2];

num[0][0][0] = 10;
num[0][0][1] = 20;

num[0][1][0] = 30;
num[0][1][1] = 40;

Let’s see java three dimensional array program,

public class ThreeDimensionalArrayDemo 
{
   public static void main(String[] args) 
   {
      int arrNum[][][] = new int[3][2][2];
      int a, b, c, temp = 1;

      for(a = 0; a < 3; a++)
      {
         for(b = 0; b < 2; b++)
         {
            for(c = 0; c < 2; c++)
            {
               arrNum[a][b][c] = temp;
               temp++;
            }
         }
      }

      for(a = 0; a < 3; a++)
      {
         for(b = 0; b < 2; b++)
         {
            for(c = 0; c < 2; c++)
            {
               System.out.print("arr[" + a + "][" + b + "][" + c + "] = " + arrNum[a][b][c] + "\t");
            }
            System.out.println();
         }
         System.out.println();
      }
   }
}

Output:

arr[0][0][0] = 1      arr[0][0][1] = 2
arr[0][1][0] = 3      arr[0][1][1] = 4

arr[1][0][0] = 5      arr[1][0][1] = 6
arr[1][1][0] = 7      arr[1][1][1] = 8

arr[2][0][0] = 9      arr[2][0][1] = 10
arr[2][1][0] = 11    arr[2][1][1] = 12


Related Posts