Java 3D Array – Three Dimensional Array

When you want to store a student’s list from a class and suppose the class consists of 100 students. By putting the data one by one into a variable will be time taking. While working with those variables and working with variables seems quite complicating and annoying. Therefore to avoid such a situation, an array is introduced, which can store data in a regular format. It is because of the array that operating on data became more accessible.

First, get a clear idea of the array, and further, we will learn the concept of the multidimensional 3D array in Java.

What is an Array?

The array is the data structure that stores data sequentially in a fixed size. The condition that imparts on the array that all the variables are of the same data type.

Despite declaring variables one by one, like number1, number 2, or number3, you can directly declare an array to store all the sequential format variables. And the format is number[3]. This declaration represents all the single variables in one pattern.

So, now look through the code, how an array is declared, and how the array process.

Declaring Array Variables

To use an array in the code, first, you need to declare a variable to refer to an array. You need to specify the data type of array, and the variable act as a reference.

Syntax:

or

For example:

or

Creating the Array

Creation of array done with new operator.

In one statement, you declaration, create and access the array’s reference to the variable.

Here my list holds 10 variables of data type double.

Processing Array

Output:

1
2
3
3
Total is 9
Max is 3

Now, the concept of an array is all about managing space for data. The idea of 2D and 3D arises to add an extra dimension to increase the space gradually.

Java 3D Array

Let’s take an example to understand the 2D and 3D array. The 2D array is the assembly of values that design to have two indexes to access the variable. Consider a chessboard with 8*8 sets of squares. A row and column number represent each square address.

3D array adds an extra dimension to the 2D array to increase the amount of space. Eventually, it is set of the 2D array. Each variable is identified with three indexes; the last two dimensions represent the number of rows and columns, and the first dimension is to select the block size. The first dimension depicts the number of tables or arrays that the 3D array contains.

Initialization of 3D Array

Similar to a 2D array, just adds one extra dimension of the same data type.

Where:

1. D1, D2, D3 is size of dimensions.

2. data_type is the data type of array element

3. array_name is the array name

3D Array Program in Java

Output:

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

Here Three loops are used i.e i, j, and k. The first loop depicts the number of the array, the j loop represents rows, and the k represents columns. We use three loops to retrieve every element from the array by specifying the index.

Leave a Reply

Your email address will not be published. Required fields are marked *