Multidimensional array in java

In Java, multidimensional arrays are actually arrays of arrays. Data in multi-dimensional arrays are stored in tabular form.

Example :

Syntax:

Data_type[1st dimension][2nd dimension]—-[Nth dimension] array_name=new data_type[size1][size2]—-[size N];

  • Where datatype Type of data to be stored in the array. For example int, char, etc.
  • Dimension: The dimension of the array created.
  • For example : 1D, 2D, etc
  • Array_name : Name of the array.
  • size1, size2, …, sizeN: Sizes of the dimensions respectively.

Example :

/* JAVA program to understand multidimensional array */
/* www.codewindow.in */

import java.util.*;
public class codewindow_one
{
    public static void main(String args[])
    {
        int arr[][]=new int[4][4];
        int count=1;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                arr[i][j]=count++;
            }
        }
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
}

Output

1  2   3   4
5  6   7   8
9  10  11  12
13 14  15  16

Latest Post

Archives

Leave a Comment

Your email address will not be published. Required fields are marked *