Java Array Guide to 3D Arrays with User Input

31
Java Array Guide to 3D Arrays with User Input
Advertisement

Java Array Guide to 3D Arrays with User Input

Arrays in Java continue to amaze as we delve into the realm of 3D arrays. In this blog post, we’ll embark on a journey to create a 3D array, print its elements, and enhance the experience with user input.

Understanding 3D Arrays in Java:

Just like a 2D array is an array of arrays, a 3D array is an array of 2D arrays. It introduces an additional dimension, forming a cube-like structure with elements accessible through three indices: depth, row, and column.

Creating a 3D Array

Java 3D Array
public class ThreeDimensionalArrayExample {
    public static void main(String[] args) {
        // Initialize a 3D array
        // This program has depth of 3, rows are 2 and 3 columns
        int[][][] threeDArray = {
            {{1, 2, 3}, {4, 5, 6}},
            {{7, 8, 9}, {10, 11, 12}},
            {{13, 14, 15}, {16, 17, 18}}
        };

        // Print the 3D array
        System.out.println("Printing 3D Array:");

        for (int i = 0; i < threeDArray.length; i++) {
            for (int j = 0; j < threeDArray[i].length; j++) {
                for (int k = 0; k < threeDArray[i][j].length; k++) {
                    System.out.print(threeDArray[i][j][k] + " ");
                }
                System.out.println(); // Move to the next line after each row
            }
            System.out.println(); // Separate each 2D array with an empty line
        }
    }
}

In this program:

  • We initialize a 3D array with predefined values.
  • Nested loops are used to iterate through each dimension of the array.
  • The program prints the elements of the 3D array in a structured format.
Output – Java 3D Array
Printing 3D Array:
1 2 3 
4 5 6 

7 8 9 
10 11 12 

13 14 15 
16 17 18 

You can modify the array values or dimensions based on your requirements.

Creating a 3D Array with User Input:

Let’s start by creating a 3D array of integers. The process involves declaring the array type, specifying the array name, and providing the size for each dimension:

Java 3D Array User Input
import java.util.Scanner;

public class ThreeDArrayExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get user input for the dimensions
        System.out.print("Enter the depth of the 3D array: ");
        int depth = scanner.nextInt();

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();

        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        // Declare and create a 3D array of integers based on user input
        int[][][] threeDArray = new int[depth][rows][columns];

        // Get user input for array elements
        System.out.println("Enter the elements of the 3D array:");
        for (int i = 0; i < depth; i++) {
            for (int j = 0; j < rows; j++) {
                for (int k = 0; k < columns; k++) {
                    System.out.print("Element at position [" + i + "][" + j + "][" + k + "]: ");
                    threeDArray[i][j][k] = scanner.nextInt();
                }
            }
        }

        // Print 3D array elements using nested loops
        System.out.println("3D Array Elements:");
        for (int i = 0; i < depth; i++) {
            for (int j = 0; j < rows; j++) {
                for (int k = 0; k < columns; k++) {
                    System.out.print(threeDArray[i][j][k] + " ");
                }
                System.out.println(); // Move to the next line for the next row
            }
            System.out.println(); // Add a line break between 2D arrays
        }
    }
}

Running the Program:

Save the code in a file named ThreeDArrayExample.java and compile it using the command:

Java
javac ThreeDArrayExample.java

Then, run the program with:

Java
java ThreeDArrayExample

Enter the depth, number of rows, columns, and the elements of the 3D array when prompted. The program will display the entered 3D array.

Output:

Output – Java 3D Array User Input
Enter the depth of the 3D array: 3
Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the 3D array:
Element at position [0][0][0]: 1
Element at position [0][0][1]: 2
Element at position [0][0][2]: 3
Element at position [0][1][0]: 4
Element at position [0][1][1]: 5
Element at position [0][1][2]: 6
Element at position [0][2][0]: 7
Element at position [0][2][1]: 8
Element at position [0][2][2]: 9
Element at position [1][0][0]: 1
Element at position [1][0][1]: 2
Element at position [1][0][2]: 3
Element at position [1][1][0]: 4
Element at position [1][1][1]: 5
Element at position [1][1][2]: 6
Element at position [1][2][0]: 7
Element at position [1][2][1]: 8
Element at position [1][2][2]: 9
Element at position [2][0][0]: 1
Element at position [2][0][1]: 2
Element at position [2][0][2]: 3
Element at position [2][1][0]: 4
Element at position [2][1][1]: 5
Element at position [2][1][2]: 6
Element at position [2][2][0]: 7
Element at position [2][2][1]: 8
Element at position [2][2][2]: 9
3D Array Elements:
1 2 3 
4 5 6 
7 8 9 

1 2 3 
4 5 6 
7 8 9 

1 2 3 
4 5 6 
7 8 9 

You can follow the Java Array: Guide to 1D and 2D Array with User Input to learn more about array. You can follow the 2D Array Use cases like Sum of Array elements and Average of Array elements to build corresponding in 3D Array.

Conclusion:

Congratulations! You’ve successfully navigated the creation and printing of a 3D array in Java, even incorporating user input for a more dynamic experience. This journey into the third dimension opens the door to advanced data structuring, making your Java programming skills even more robust. Happy coding!

Latest Posts: