Skip to content

Latest commit

 

History

History
856 lines (500 loc) · 9.13 KB

File metadata and controls

856 lines (500 loc) · 9.13 KB

3-Dimensional Array in Java — DOUBT KILLER 🔥

The biggest confusion in a 3-D array is understanding what each [] means, what each index means, and what each length means. Let's kill those doubts one by one.


  1. First: What Is a 3-D Array?

A 3-D array stores data in three dimensions:

Layer → Row → Column

Syntax:

int[][][] a;

Access:

a[layer][row][column]

or:

a[i][j][k]

Remember:

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


  1. Don't Think of It as a Mysterious Cube

For beginners, "3-D" can sound complicated.

Instead, think:

1-D → One line 2-D → One table 3-D → Multiple tables

For example:

Layer 0

10 20 30 40 50 60

Layer 1

70 80 90 100 110 120

Each layer is a 2-D array.

Together, the layers form a 3-D array.


  1. Why Do We Write [][][]?

Look at the progression:

int[] // 1-D int[][] // 2-D int[][][] // 3-D

Therefore:

[] → 1 dimension [][] → 2 dimensions [][][] → 3 dimensions

And access becomes:

a[i] // 1-D a[i][j] // 2-D a[i][j][k] // 3-D


  1. What Does This Mean?

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

It means:

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

So:

2 × 3 × 4 = 24 elements


  1. What Does a.length Mean?

This is a very common doubt.

For:

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

a.length

means:

Number of layers.

Therefore:

a.length = 2

❌ It does NOT mean 24.


  1. What Does a[i].length Mean?

a[i].length

means:

Number of rows in layer i.

For:

new int[2][3][4]

the answer is:

a[i].length = 3


  1. What Does a[i][j].length Mean?

This means:

Number of elements in row j of layer i.

For:

new int[2][3][4]

the answer is:

a[i][j].length = 4

🔥 Memorize this:

a.length ↓ Layers

a[i].length ↓ Rows

a[i][j].length ↓ Columns


  1. What Does a[i][j][k] Mean?

It represents one actual element.

a[i][j][k]

i → Which layer? j → Which row? k → Which column?

Example:

a[1][0][2]

means:

Layer 1 ↓ Row 0 ↓ Column 2


  1. Let's Trace a Real Example

Consider:

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

Visual:

Layer 0

Row 0 → 10 20 Row 1 → 30 40

Layer 1

Row 0 → 50 60 Row 1 → 70 80

Now:

a[1][0][1]

Step 1:

1 → Layer 1

Step 2:

0 → Row 0

Step 3:

1 → Column 1

Therefore:

60


  1. What Is a[1]?

This is another important doubt.

Given:

int[][][] a;

Then:

a[1]

does not give one integer.

It gives the entire 2-D layer.

Its type is:

int[][]

So:

a[1] → int[][]


  1. What Is a[1][0]?

Now we've selected:

Layer 1 Row 0

So:

a[1][0]

represents one complete row.

Its type is:

int[]

Therefore:

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

This is a very important concept.


  1. Why Is It Called "Array of Arrays"?

Because:

int[][][] a;

can be understood as:

int[][][] ↓ array of ↓ int[][] ↓ array of ↓ int[] ↓ array of ↓ int

So a 3-D array is essentially:

An array of 2-D arrays.

And each 2-D array is:

An array of 1-D arrays.


  1. Why Do We Need Three Loops?

Because we have three levels to visit:

Layer ↓ Row ↓ Column

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.println(a[i][j][k]);
    }
}

}

Remember:

i → Layer j → Row k → Column


  1. Why Not Use a.length for All Three Loops?

❌ Wrong:

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

Why?

Because the dimensions can have different sizes.

For:

new int[2][3][4]

the sizes are:

Layers = 2 Rows = 3 Columns = 4

Therefore:

First → a.length Second → a[i].length Third → a[i][j].length


  1. Is Every 3-D Array Rectangular?

❌ No.

Java supports jagged arrays.

Example:

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

Here:

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

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

This is completely legal.


  1. Why Does Jagged Array Work?

Because each row is an independent array.

Conceptually:

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

That's why:

a[i][j].length

is the safest way to find the number of elements in the current row.


  1. What Happens When We Create an int 3-D Array?

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

All elements initially contain:

0

So conceptually:

Layer 0

0 0 0 0 0 0 0 0 0 0 0 0

Layer 1

0 0 0 0 0 0 0 0 0 0 0 0


  1. Can We Change an Element?

Yes.

a[1][2][3] = 100;

This means:

Layer 1 Row 2 Column 3

contains:

100


  1. Can We Store Strings?

Yes.

String[][][] names = { { {"A", "B"}, {"C", "D"} }, { {"E", "F"}, {"G", "H"} } };

Then:

System.out.println(names[1][0][1]);

Output:

F

The concept doesn't change.


  1. Can We Use Enhanced for?

Yes.

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

for (int[] row : layer) {

    for (int value : row) {

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

    System.out.println();
}

System.out.println();

}

Understand:

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

This is actually an excellent way to understand the nested-array structure.


  1. Find the Sum

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);

For:

10 20 30 40

50 60 70 80

the result is:

360


  1. Find 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);


  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();
    }
}

}

Call:

display(a);


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

Yes.

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

return new int[][][] {
    {
        {10, 20},
        {30, 40}
    },
    {
        {50, 60},
        {70, 80}
    }
};

}

Then:

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


  1. Common Errors

❌ Error 1 — Wrong index

a[2][0][0]

If there are only 2 layers:

Valid layers → 0, 1

Layer 2 doesn't exist.

Result:

ArrayIndexOutOfBoundsException


❌ Error 2 — Confusing index order

Don't think:

a[column][row][layer]

The normal conceptual order is:

a[layer][row][column]


❌ Error 3 — Thinking a[0] is an integer

It isn't.

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


❌ Error 4 — Confusing length

Remember:

a.length → Layers a[i].length → Rows a[i][j].length → Columns


🔥 26. 3-D Array vs 2-D Array

2-D Array 3-D Array

int[][] int[][][] Row + Column Layer + Row + Column a[i][j] a[i][j][k] Usually 2 loops Usually 3 loops One table Multiple tables

Think:

2-D: TABLE

3-D: TABLE + TABLE + TABLE


🧠 27. The Ultimate Mental Model

Imagine a book.

Book ↓ Pages ↓ Rows ↓ Columns

For our 3-D array, simplify it to:

Layers ↓ Rows ↓ Columns

Then:

a[i][j][k]

means:

Go to layer i, then row j, then column k.


🏆 FINAL DOUBT-KILLER

Question Answer

What is a 3-D array? Array of 2-D arrays Syntax? int[][][] a Number of indexes? 3 First index? Layer Second index? Row Third index? Column Access syntax? a[i][j][k] a.length? Number of layers a[i].length? Number of rows a[i][j].length? Number of elements in current row Usually how many loops? 3 Can it be jagged? Yes Can it be passed to a method? Yes Can it be returned from a method? Yes Can it store objects/String? Yes

⭐ The 6 rules to memorize

  1. int[][][] → 3-D array

  2. a[i][j][k] → access one element

  3. i → Layer

  4. j → Row

  5. k → Column

  6. a.length → Layers a[i].length → Rows a[i][j].length → Columns

🔥 One final picture

3-D ARRAY │ ┌──────┴──────┐ ↓ ↓ Layer 0 Layer 1 │ │ ┌──┴──┐ ┌──┴──┐ ↓ ↓ ↓ ↓ Row Row Row Row │ │ │ │ Columns Columns Columns Columns

If you understand a[i][j][k] = Layer → Row → Column, you understand the core of 3-D arrays in Java.