Blog

Two Dimensional Array in Java | Basic Java Tutorial

Two Dimensional Array in Java | Basic Java Tutorial

Two Dimensional Array, data stored in row and columns, and we can access the record using both the row index and column index.

  • In Simple word, it can be consider as array of arrays.
  • Stored in tabular form ( in row major order).

Data_Type[][] Array_Name;
  • Data_type decides the type of elements it will accept. For example, If we want to store integer values, then the Data Type will be declared as int. If we want to store Float values, then the Data Type will be float.
  • Array_Name: This is the name to give it to this Java two dimensional array. For example, Car, students, age, marks, department, employees, etc.

Example to demonstrate Two dimensional array in java

package practice_11;
public class TwoDArray
{    
    public static void main(String[] args)
    {
        int[][] arr = {         //jagged array
                        {1,2,3},
                        {4,5,6,7,8},
                        {7,8,9,10}
                      };
        //accessing value of 2DArray
//        System.out.println(arr[0][0]);
//        System.out.println(arr[0][1]);
        
        for(int i =0; i<arr.length; i++)
        {
            for(int j =0; j<arr[i].length; j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        
    }
    
}