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:
dataType[] arrayRefVar; // suitable way.
or
dataType arrayRefVar[]; // executes but not a suitable way.
For example:
double[] myList; // suitable way
or
double myList[]; // executes but not a suitable way.
Creating the Array
Creation of array done with new operator.
dataType[] arrayRefVar = new dataType[arraySize];
In one statement, you declaration, create and access the array’s reference to the variable.
double[] myList = new double[10]
Here my list holds 10 variables of data type double.
Processing Array
public class array { public static void main(String[] args) { int[]arrayl = {1, 2, 3, 3}; // Printing all the elements for (int i = 0; i < arrayl.length; i++) { System.out.println(arrayl[i] + " "); } // Sum of all elements int total = 0; for (int i = 0; i < arrayl.length; i++) { total += arrayl[i]; } System.out.println("Total is " + total); // Finding the largest element int max = arrayl[0]; for (int i = 1; i < arrayl.length; i++) { if (arrayl[i] > max) max = arrayl[i]; } System.out.println("Max is " + max); } }
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.
int[][] myarray = new int[3][3]
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.
data_type [] [] [] array_name = new data_type [d1][d2][d3];
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
public class Array { public static void main(String[] args) { int[][][] student_arr= { {{1, 2, 3},{2, 3, 4}}, {{4, 5, 6},{1, 7, 8},} }; // retrieve all the elements from 3D array int i, j, k; for (i = 0; i<2; i++) { for (j= 0; j<2; j++) { for (k= 0; k<3; k++) { System.out.print("student_arr[" +i+ "][" +j+"]["+k+ "] = "+student_arr[i][j][k]+ "\t"); } System.out.println(); } System.out.println(); } } }
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.