3D Arrays And Beyond
Part of the Logic & Flow section of Coddy's Java journey — lesson 7 of 59.
Just as you can create 2D arrays (arrays of arrays), you can create 3D arrays (arrays of arrays of arrays), 4D arrays, and so on. These are called multidimensional arrays.
A 3D array can be visualized as a collection of 2D arrays stacked on top of each other, forming a cube-like structure. Each element in a 3D array is accessed using three indices: one for the depth (which 2D array), one for the row, and one for the column.
Here's how you declare and initialize a 3D array in Java:
data_type[][][] array_name = new data_type[depth][rows][columns];data_type: The type of elements the array will hold.array_name: The name of the array.
depth: The number of 2D arrays (depth).rows: The number of rows in each 2D array.columns: The number of columns in each 2D array.
For example, to create a 3D array of integers with dimensions 2x3x4, you would write:
int[][][] cube = new int[2][3][4];You can also initialize a 3D array directly:
int[][][] cube = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};Challenge
EasyWrite a program that gets that takes three integer arguments: depth, rows, and cols. It should create a 3D array of integers with the specified dimensions and Initialize each element of the array with the product of its depth, row, and column indices (starting from 1).
For example, the element at [d][r][c] should have the value (d + 1) * (r + 1) * (c + 1).
Finally print the resulting 3D array to the console using nested loops. The output should look like this:
Depth 0:
1 2 3 4
2 4 6 8
3 6 9 12
Depth 1:
2 4 6 8
4 8 12 16
6 12 18 24Cheat sheet
3D arrays are arrays of arrays of arrays, visualized as cube-like structures with depth, rows, and columns.
Declaration and initialization:
data_type[][][] array_name = new data_type[depth][rows][columns];Example:
int[][][] cube = new int[2][3][4];Direct initialization:
int[][][] cube = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};Elements are accessed using three indices: array[depth][row][column]
Try it yourself
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int depth = scanner.nextInt();
int rows = scanner.nextInt();
int cols = scanner.nextInt();
int[][][] array3D = new int[depth][rows][cols];
// Write your code below
// Print the 3D array
for (int d = 0; d < depth; d++) {
System.out.println("Depth " + d + ":");
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
System.out.print(array3D[d][r][c] + " ");
}
System.out.println();
}
}
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays2HashMap Part 1
What is a HashMap?Declare a HashMapAccessing ValuesCheck If Key ExistsModifying DictionariesRecap - HashMap