Skip to main content

Multi Dimensional Arrays

✔ It is an Array of Arrays.

TypeDescription
Single Dimensional ArrayCollection of actual values
Two Dimensional ArrayCollection of single dimensional arrays
Three Dimensional ArrayCollection of 2-D arrays

2.10.5 Two Dimensional Arrays

✔ Two Dimensional Arrays can be called as 2-D array.

2.10.5.1 Array Declaration

Syntax

[modifiers]<dataType> <refVarName>[][];
[modifiers]<dataType> [][]<refVarName>;
[modifiers]<dataType> [][]<refVarName>;
[modifiers]<dataType> []<refVarName>[];
[modifiers]<dataType> [] <refVarName>[];

Examples

int arr[][];

int[][] arr;

String[] names[];

2.10.5.2 Array Construction

Syntax

<refVarName>=new <dataType>[<size1>][<size2>];

Explanation

  • <size1> → number of arrays (rows)
  • <size2> → number of elements in each array (columns)

Examples

arr=new int[3][2];
names=new String[4][3];

💡 Quick Understanding

  • 2D array = array of arrays
  • Think like a table (rows × columns)
  • First size → rows
  • Second size → columns

2.10.5.3 Array Initialization

✔ You can initialize array elements with your own value using index representation.

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

✔ Explanation

Here we are creating 3 arrays, each with size 2, and these three array addresses will be stored in the main array.

Default Values (before initialization)

arr[0][0] -> Represents the 1st element of 1st array which contains value 0
arr[0][1] -> Represents the 2nd element of 1st array which contains value 0

arr[1][0] -> Represents the 1st element of 2nd array which contains value 0
arr[1][1] -> Represents the 2nd element of 2nd array which contains value 0

arr[2][0] -> Represents the 1st element of 3rd array which contains value 0
arr[2][1] -> Represents the 2nd element of 3rd array which contains value 0

✔ Initializing Values

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

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

arr[2][0]=50;
arr[2][1]=60;

✔ Accessing the Elements After Initialization

arr[0][0] -> Represents the 1st element of 1st array which contains value 10
arr[0][1] -> Represents the 2nd element of 1st array which contains value 20

arr[1][0] -> Represents the 1st element of 2nd array which contains value 30
arr[1][1] -> Represents the 2nd element of 2nd array which contains value 40

arr[2][0] -> Represents the 1st element of 3rd array which contains value 50
arr[2][1] -> Represents the 2nd element of 3rd array which contains value 60

💡 Quick Understanding

  • 2D array = array of arrays
  • arr[i][j] → i = row, j = column
  • Default values → 0 (for int)
  • You can assign values manually using indexes

2.10.5.4 Array Declaration and Construction

Syntax

[modifiers]<dataType> <refVarName>[][]=new <dataType>[<size1>][<size2>];
[modifiers]<dataType> [][]<refVarName>=new <dataType>[<size1>][<size2>];

Examples

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

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

2.10.5.5 Array Declaration, Construction and Initialization

Syntax

<dataType><refVarName>[][]= { {v1,v2,...}, {v1,v2,...}, {v1,v2,...}, ... };

Examples

int arr[][] = { { 10, 20 }, { 30, 40 }, { 50, 60 } };

int arr[][] = { { 10, 20, 30 }, { 40 }, { 50, 60, 70, 80 }, { 90, 100 } };

Lab276.java

class Lab276 {

public static void main(String[] args) {
int arr[][] = null;

arr = new int[3][2];

System.out.println("Len :" + arr.length);
System.out.println("Len :" + arr[0].length);
System.out.println("Len :" + arr[1].length);
System.out.println("Len :" + arr[2].length);
}
}

Lab277.java

class Lab277 {

public static void main(String[] args) {
int arr[][] = null;

arr = new int[3][2];

arr[0][0] = 10;
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40;
arr[2][0] = 50;
arr[2][1] = 60;

System.out.println("Using Normal For loop");

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print("\t" + arr[i][j]);
}
System.out.println();
}

System.out.println("\nUsing Enhanced for loop");

for (int a[] : arr) {
for (int a1 : a) {
System.out.print("\t" + a1);
}
System.out.println();
}
}
}

💡 Quick Understanding

  • arr.length → number of rows
  • arr[i].length → number of columns in each row
  • 2D arrays can be irregular (jagged)
  • You can use:
    • Normal for loop
    • Enhanced for-each loop

Lab278.java

class Lab278 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[][]; // invalid (size must be specified)
System.out.println("L : " + arr.length);
}
}

Lab279.java

class Lab279 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[][3]; // invalid (first dimension required)
System.out.println("Len : " + arr.length);
}
}

Lab280.java

class Lab280 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[3][]; // ✔ valid

System.out.println("Len : " + arr.length);

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Lab281.java

class Lab281 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[3][2];

System.out.println("Len : " + arr.length);

arr[0] = 1234; // invalid (expects int[])
System.out.println(arr[0]);
}
}

Lab282.java

class Lab282 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[3][2];

arr[0][0] = 1234;
System.out.println(arr[0][0]);
}
}

Lab283.java

class Lab283 {

public static void main(String[] args) {
int arr[][] = null;
arr = new int[3][];

int tmp[] = { 99, 88, 77, 66, 55 };

System.out.println("L : " + arr.length);

arr[0] = new int[4]; // ✔ valid
arr[1] = new int[] { 10, 30, 20 }; // ✔ valid
arr[2] = tmp; // ✔ valid

for (int ar[] : arr) {
for (int a : ar) System.out.print("\t" + a);
System.out.println();
}
}
}

Lab284.java

class Lab284 {

public static void main(String[] args) {
int arr[][] = {
{ 10, 23, 21 },
{ 12 },
{ 23, 43, 12, 32 },
{ 98, 34, 32 },
};

for (int ar[] : arr) {
for (int a : ar) System.out.print("\t" + a);
System.out.println();
}
}
}

Lab285.java

class Lab285 {

public static void main(String[] args) {
int arr[][] = null;

arr = new int[][] {
{ 10, 23, 21 },
{ 12 },
{ 23, 43, 12, 32 },
{ 98, 34, 32 },
};

for (int ar[] : arr) {
for (int a : ar) System.out.print("\t" + a);
System.out.println();
}
}
}

💡 Quick Understanding

  • new int[][] → size required
  • new int[][3] → first dimension mandatory
  • new int[3][] ✔ → allowed (jagged array)
  • arr[0] → must be int[], not int
  • 2D arrays in Java are jagged (rows can have different sizes)