Skip to main content

3D Arrays

3D: Three-Dimensional Arrays

Master 3D arrays: cube-like structures with layer[row][column] access patterns. Learn initialization methods, traversal techniques, memory optimization, and real-world applications like RGB images, game worlds, and scientific data modeling.

What are 3D arrays?

Three-dimensional (3D) arrays can be defined as "arrays of arrays of arrays" or a collection of 2D arrays. It is a cube-like structure where elements are organized in three dimensions: depth (layers), rows, and columns.

Building upon our understanding of 2D arrays, you can think of 3D arrays as multiple 2D grids stacked on top of each other, like pages in a book or floors in a building. Each "layer" or "page" contains a complete 2D array.

Like normal arrays and 2D arrays, 3D arrays are static (fixed-size) in Java. In memory, items are organized sequentially, one after another, following a specific order pattern.

The items could be Integer, String, Object, or any data type. The items are stored in contiguous (adjacent to each other) memory locations, making them cache-friendly for sequential access patterns.

Illustration

A simple sketch with D layers (depth), N rows, and M columns as follows. To access elements, you need to query using its corresponding layer index, row index, and column index.

 Layer 0          Layer 1          Layer 2
 [0][0][0]       [1][0][0]       [2][0][0]
┌─────────┐     ┌─────────┐     ┌─────────┐
│ 1  2  3 │     │ 10 11 12│     │ 19 20 21│
│ 4  5  6 │     │ 13 14 15│     │ 22 23 24│
│ 7  8  9 │     │ 16 17 18│     │ 25 26 27│
└─────────┘     └─────────┘     └─────────┘

The following represents the same 3D array with complete indexing notation for easy reference:

cube[0][0][0] = 1   cube[0][0][1] = 2   cube[0][0][2] = 3
cube[0][1][0] = 4   cube[0][1][1] = 5   cube[0][1][2] = 6
cube[0][2][0] = 7   cube[0][2][1] = 8   cube[0][2][2] = 9

cube[1][0][0] = 10  cube[1][0][1] = 11  cube[1][0][2] = 12
cube[1][1][0] = 13  cube[1][1][1] = 14  cube[1][1][2] = 15
cube[1][2][0] = 16  cube[1][2][1] = 17  cube[1][2][2] = 18

cube[2][0][0] = 19  cube[2][0][1] = 20  cube[2][0][2] = 21
cube[2][1][0] = 22  cube[2][1][1] = 23  cube[2][1][2] = 24
cube[2][2][0] = 25  cube[2][2][1] = 26  cube[2][2][2] = 27

Declaration and initialization

To declare a 3D array, you need to specify the data type of the elements and the three dimensions. The general declaration of a 3D array is:

dataType[][][] arrayName = new dataType[depth][rows][columns];

Here's an example of creating a 3D array of integers with 3 layers, 4 rows, and 4 columns. To access the last element of the 3D array, we use A[2][3][3].

With this knowledge, if there are D layers, N rows, and M columns, then the array holds D × N × M elements in it.

There are several ways we can declare and initialize 3D arrays in Java.

Approach 1: Step-by-step initialization

// Create a 3D array with 2 layers, 3 rows, 3 columns
int[][][] cube = new int[2][3][3];

// Initialize layer 0
cube[0][0][0] = 1; cube[0][0][1] = 2; cube[0][0][2] = 3;
cube[0][1][0] = 4; cube[0][1][1] = 5; cube[0][1][2] = 6;
cube[0][2][0] = 7; cube[0][2][1] = 8; cube[0][2][2] = 9;

// Initialize layer 1
cube[1][0][0] = 10; cube[1][0][1] = 11; cube[1][0][2] = 12;
cube[1][1][0] = 13; cube[1][1][1] = 14; cube[1][1][2] = 15;
cube[1][2][0] = 16; cube[1][2][1] = 17; cube[1][2][2] = 18;

Approach 2: Inline initialization

// More compact initialization
int[][][] cube = {
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    },
    {
        {10, 11, 12},
        {13, 14, 15},
        {16, 17, 18}
    }
};

Approach 3: Using loops for dynamic initialization

int[][][] cube = new int[3][4][5];
int value = 1;

for (int layer = 0; layer < 3; layer++) {
    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 5; col++) {
            cube[layer][row][col] = value++;
        }
    }
}

This creates a 3D array named cube with 2 layers, 3 rows, and 3 columns. The indexes of the elements in the array range from 0 to (depth-1) for layers, 0 to (rows-1) for rows, and 0 to (columns-1) for columns.

3D arrays in Java are useful for representing data that has three logical dimensions, such as:

  • RGB color values in images (width × height × color channels)
  • 3D game worlds (x × y × z coordinates)
  • Time-series data with multiple categories (time × category × metrics)
  • Scientific simulations (3D space modeling)

Arrays with Different Data Types

3D arrays work with various data types:

// String arrays for storing text data in 3D structure
String[][][] textCube = new String[2][3][4];

// Character arrays for 3D text processing
char[][][] charCube = new char[5][5][5];

// Boolean arrays for 3D logical operations
boolean[][][] flagCube = new boolean[3][3][3];

// Object arrays for complex data structures
Person[][][] peopleCube = new Person[2][4][6];