Skip to content

Latest commit

 

History

History
1073 lines (642 loc) · 12.8 KB

File metadata and controls

1073 lines (642 loc) · 12.8 KB

3-Dimensional Array in Java — DEEPDIVE

A 3-Dimensional Array is the next level after 1-D and 2-D arrays.

The easiest way to understand it is:

1-D → Line 2-D → Table 3-D → Collection of Tables / Layers


  1. What Is a 3-Dimensional Array?

A 3-D array stores data using three indexes.

int[][][] a;

An element is accessed using:

a[layer][row][column]

So:

a[i][j][k] │ │ │ │ │ └── Column │ └───── Row └──────── Layer

Simple visualization

Suppose we have two 2-D tables:

Layer 0

┌────┬────┬────┐ │ 10 │ 20 │ 30 │ ├────┼────┼────┤ │ 40 │ 50 │ 60 │ └────┴────┴────┘

Layer 1

┌────┬────┬────┐ │ 70 │ 80 │ 90 │ ├────┼────┼────┤ │100 │110 │120 │ └────┴────┴────┘

Together, these form a 3-D array.


  1. Connection Between 1-D, 2-D and 3-D

This is the easiest way to build the concept.

1-D

int[] a;

Think:

10 20 30 40

One index:

a[i]


2-D

int[][] a;

Think:

10 20 30 40 50 60 70 80 90

Two indexes:

a[i][j]


3-D

int[][][] a;

Think:

Layer 0 Layer 1

10 20 30 70 80 90 40 50 60 100 110 120

Three indexes:

a[i][j][k]

Therefore:

1-D → a[i]

2-D → a[i][j]

3-D → a[i][j][k]


  1. Declaration

The preferred declaration is:

int[][][] a;

This only declares a reference variable.

It does not create the actual array.

You can also write:

int a[][][];

or:

int[] a[][];

But the clearest style is:

int[][][] a;


  1. Creation

To create the array:

int[][][] a = new int[2][3][4];

This means:

2 → Layers 3 → Rows in each layer 4 → Elements in each row

Visual:

Layer 0 Layer 1

┌──┬──┬──┬──┐ ┌──┬──┬──┬──┐ │ │ │ │ │ Row 0 │ │ │ │ │ Row 0 ├──┼──┼──┼──┤ ├──┼──┼──┼──┤ │ │ │ │ │ Row 1 │ │ │ │ │ Row 1 ├──┼──┼──┼──┤ ├──┼──┼──┼──┤ │ │ │ │ │ Row 2 │ │ │ │ │ Row 2 └──┴──┴──┴──┘ └──┴──┴──┴──┘

Total elements:

2 × 3 × 4 = 24


  1. Direct Initialization

Instead of using new, we can initialize directly:

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

Think:

Layer 0:

10 20 30 40

Layer 1:

50 60 70 80


  1. Understanding a[0][0][0]

This is the most important part.

Given:

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

Find:

a[0][0][0]

Follow the indexes:

First 0 → Layer 0 Second 0 → Row 0 Third 0 → Column 0

Therefore:

a[0][0][0] = 10


  1. Understanding a[1][0][1]

Now:

a[1][0][1]

means:

1 → Layer 1

0 → Row 0

1 → Column 1

Layer 1:

50 60 70 80

Row 0:

50 60

Column 1:

60

Therefore:

a[1][0][1]

is:

60


  1. The Three Dimensions

Always remember:

a[i][j][k]

i → Layer j → Row k → Column

A useful memory trick:

First choose the table, then the row, then the column.

Layer → Row → Column


  1. Understanding length

This is extremely important.

Suppose:

int[][][] a = new int[2][3][4];

a.length

a.length

means:

Number of layers.

Answer:

2


a[i].length

a[i].length

means:

Number of rows in layer i.

Answer:

3


a[i][j].length

a[i][j].length

means:

Number of elements in row j of layer i.

Answer:

4

Therefore:

a.length ↓ Layers

a[i].length ↓ Rows

a[i][j].length ↓ Columns


  1. Traversing a 3-D Array

Since there are three dimensions, we normally need three nested loops.

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[i].length; j++) {

    for (int k = 0; k < a[i][j].length; k++) {

        System.out.print(a[i][j][k] + " ");
    }

    System.out.println();
}

System.out.println();

}

Understand the roles:

Outer loop → Layer Middle loop → Row Inner loop → Column


  1. Complete Program

class ThreeDArray {

public static void main(String[] args) {

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

    for (int i = 0; i < a.length; i++) {

        for (int j = 0; j < a[i].length; j++) {

            for (int k = 0; k < a[i][j].length; k++) {

                System.out.print(a[i][j][k] + " ");
            }

            System.out.println();
        }

        System.out.println();
    }
}

}

Output:

10 20 30 40

50 60 70 80

The blank line separates the layers.


  1. Trace the Nested Loops

Suppose:

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

The loops execute approximately like this:

i = 0 j = 0 k = 0 → 10 k = 1 → 20

j = 1
    k = 0 → 30
    k = 1 → 40

i = 1 j = 0 k = 0 → 50 k = 1 → 60

j = 1
    k = 0 → 70
    k = 1 → 80

So the order is:

10 → 20 → 30 → 40 → 50 → 60 → 70 → 80


  1. Why Three Loops?

Because we have three dimensions.

Compare:

1-D ↓ one dimension ↓ one loop

2-D ↓ two dimensions ↓ two nested loops

3-D ↓ three dimensions ↓ three nested loops

General pattern:

Dimension Typical traversal

1-D 1 loop

2-D 2 nested loops

3-D 3 nested loops


  1. 3-D Array Is Also an Array of Arrays of Arrays

This is the deeper Java concept.

A:

int[][][] a;

can be understood as:

Array ↓ contains int[][] ↓ each int[][] contains int[] ↓ each int[] contains int

Conceptually:

a │ ├── Layer 0 → int[][] │ ├── Row 0 → int[] │ ├── Row 1 → int[] │ └── Row 2 → int[] │ └── Layer 1 → int[][] ├── Row 0 → int[] ├── Row 1 → int[] └── Row 2 → int[]

Therefore:

A 3-D array in Java is an array of 2-D arrays, and each 2-D array is an array of 1-D arrays.


  1. Can a 3-D Array Be Jagged?

Yes.

Because Java arrays are arrays of arrays.

For example:

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

Notice the lengths are different.

Layer 0 Row 0 → 2 elements Row 1 → 3 elements

Layer 1 Row 0 → 1 element Row 1 → 3 elements

This is valid Java.


  1. Why a[i][j].length Is Important

Because different rows can have different lengths.

Therefore:

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[i].length; j++) {

    for (int k = 0; k < a[i][j].length; k++) {

        System.out.print(a[i][j][k] + " ");
    }
}

}

is more flexible than assuming fixed dimensions.


  1. Creating a 3-D Jagged Array Manually

You can create it step by step:

int[][][] a = new int[2][][];

a[0] = new int[2][]; a[1] = new int[3][];

a[0][0] = new int[2]; a[0][1] = new int[4];

a[1][0] = new int[1]; a[1][1] = new int[3]; a[1][2] = new int[2];

This demonstrates how flexible Java's multidimensional arrays are.


  1. Default Values

When you create:

int[][][] a = new int[2][3][4];

all elements initially contain:

0

For example:

System.out.println(a[0][0][0]);

Output:

0

Other primitive/reference types have their normal Java default values.

Data type Default

int 0 double 0.0 char '\u0000' boolean false Reference types null


  1. Finding the Sum of a 3-D Array

Example:

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

int sum = 0;

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[i].length; j++) {

    for (int k = 0; k < a[i][j].length; k++) {

        sum += a[i][j][k];
    }
}

}

System.out.println("Sum = " + sum);

Output:

Sum = 360

Because:

10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 = 360


  1. Finding the Largest Element

int largest = a[0][0][0];

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[i].length; j++) {

    for (int k = 0; k < a[i][j].length; k++) {

        if (a[i][j][k] > largest) {
            largest = a[i][j][k];
        }
    }
}

}

System.out.println("Largest = " + largest);


  1. Enhanced for Loop

A 3-D array can also be traversed with enhanced for loops:

for (int[][] layer : a) {

for (int[] row : layer) {

    for (int value : row) {

        System.out.print(value + " ");
    }

    System.out.println();
}

System.out.println();

}

Understand the types:

a ↓ int[][] layer ↓ int[] row ↓ int value

This is a beautiful demonstration of the fact that Java's multidimensional arrays are nested arrays.


  1. Accessing a Particular Layer

Suppose:

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

Then:

a[0]

represents the entire first 2-D layer.

Its type is:

int[][]

Similarly:

a[0][1]

represents one row.

Its type is:

int[]

And:

a[0][1][0]

represents one element.

Its type is:

int

Therefore:

a[0] → int[][] a[0][1] → int[] a[0][1][0] → int

This is an important concept.


  1. a.length vs a[i].length vs a[i][j].length

Memorize this table:

Expression Meaning

a.length Number of layers a[i].length Number of rows in layer i a[i][j].length Number of elements in row j a[i][j][k] Actual element

For:

int[][][] a = new int[2][3][4];

we get:

a.length → 2 a[0].length → 3 a[0][0].length → 4


  1. Common Mistake — Using the Wrong length

A beginner might write:

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

    for (int k = 0; k < a.length; k++) {
        System.out.println(a[i][j][k]);
    }
}

}

This is wrong because all three dimensions don't necessarily have the same size.

Correct:

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[i].length; j++) {

    for (int k = 0; k < a[i][j].length; k++) {

        System.out.println(a[i][j][k]);
    }
}

}

The pattern:

1st dimension → a.length 2nd dimension → a[i].length 3rd dimension → a[i][j].length


  1. Can We Pass a 3-D Array to a Method?

Yes.

static void display(int[][][] a) {

for (int[][] layer : a) {

    for (int[] row : layer) {

        for (int value : row) {

            System.out.print(value + " ");
        }

        System.out.println();
    }

    System.out.println();
}

}

Call it:

int[][][] x = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };

display(x);


  1. Can a Method Return a 3-D Array?

Yes.

static int[][][] getArray() {

return new int[][][] {
    {
        {1, 2},
        {3, 4}
    },
    {
        {5, 6},
        {7, 8}
    }
};

}

Then:

int[][][] a = getArray();


  1. Real-Life Concept

A 3-D array can be imagined as:

Building → Floor → Room → Data

or:

Image data → Layer → Row → Pixel

or:

School → Class → Student → Marks

For example:

School ↓ Class ↓ Student

The actual application may use objects or collections instead, but the hierarchy helps understand the three dimensions.


  1. 1-D vs 2-D vs 3-D

Feature 1-D 2-D 3-D

Declaration int[] int[][] int[][][] Indexes 1 2 3 Access a[i] a[i][j] a[i][j][k] Structure Line Table Layers Typical loops 1 2 3 Java concept Array Array of arrays Array of arrays of arrays


  1. The Most Important Concept

Don't think of:

int[][][] a

as some mysterious special "cube" data structure.

Think:

int[][][] ↓ Array of ↓ int[][] ↓ Array of ↓ int[] ↓ Array of ↓ int

That explains almost everything about 3-D arrays in Java.


🧠 FINAL MEMORY MAP

3-D ARRAY │ ↓ int[][][] a │ ┌────────┴────────┐ ↓ ↓ Layer Layer │ ↓ Rows │ ↓ Columns

Remember:

a[i][j][k]

i → Layer j → Row k → Column

And:

a.length

→ Layers

a[i].length

→ Rows in layer i

a[i][j].length

→ Columns/elements in row j

⭐ One-line definition

A 3-Dimensional Array in Java is an array of 2-Dimensional arrays, used to organize data using three indexes: layer, row, and column.