Home > Java > javaTutorial > 3D Arrays in Java

3D Arrays in Java

WBOY
Release: 2024-08-30 15:27:26
Original
1242 people have browsed it

Before understanding 3D arrays in Java, we should know what the array is and why is it used in programming languages? Arrays are basically a group of similar type of values which is referred by the same name. By similar type, we mean the values of the same datatype. Consider a situation in which we want to store the names of all the students of a class. As the name of Student is of String data type, but it would be incorrect to store the name of each student in a different variable as it would not only occupy a lot of space but also creates confusion in a program too by increasing almost the same lines of code. So to handle these types of situations, Arrays are used. The programmer can create an array of Student_names and specify its size at the time of the array object creation. In this way, there would be no need to specify the variable name to each student name, and whenever we want to update, insert and retrieve the values, indices of this array can be used.

In Java, an array variable is declared similar to the other variables with [] sign after the data type of it. Array size needs to be defined at the time of array creation, and it remains constant. Array elements are accessed by the numeric indexes, with the first element stored at 0 indexes. There are basically two types of arrays in Java, i.e. one-dimensional and multi-dimensional arrays. 3D arrays fall under the category of multidimensional arrays. Multidimensional arrays, in simple words, can be defined as an array of arrays, and 3D arrays are an array of 2D arrays. 3D is a complex form of multidimensional arrays. Consider a scenario of Apartments in a building. Suppose there are 10 floors in the apartment and each floor has 5 flats, and each flat has 3 rooms. To handle this data in programming, 3D arrays are used.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

How 3D Arrays are Defined in Java?

Java uses a very simple way to define the arrays. Square brackets (‘[ ]’) are used to define the array object after the data type of array. One needs to define the size at the time of the declaration of the array. 3D arrays are defined with three brackets. Below given is the syntax of defining the 3D arrays in Java:

Data_type array_name[ ] [ ] [ ] = new array_name[a][b][c];
Copy after login
  • Here data_type: data type of elements that will be stored in the array. array_name: name of the array
  • new: keyword to create an object in Java
  • a, b, c: holds the numeric values for the various dimensions.

Syntax:

int [ ] [ ] [ ] arr = new int [10][4][3];
Copy after login

There can be a maximum of 10x4x3 = 120 elements stored by the array ‘arr’ in the above example.

How to Create 3D Arrays and Insert values in them in Java?

Creating 3D arrays in Java is as simple as creating 1D and 2D arrays. As mentioned above, it is important to define the size of an array at the time of declaration. Creating 3D arrays involves one more step of passing/ entering values in them in the form of an array of 2D arrays. We can define the size of an array and can insert/ enter the values afterwards, or we can directly pass the values in an array. So the manner of value defined in 3D arrays is given below:

Syntax

data_type[][][] arr_name =
{
{
{Array1Row1Col1,Array1Row1Col2,....},
{Array1Row2Col1, Array1Row2Col2, ....}
},
{
{Array2Row1Col1, Array2Row1Col2, ....},
{Array2Row2Col1, Array2Row2Col2, ....}
}
}
Copy after login

Code

int num_array [ ] [ ] [ ] = {
{
{10 ,20 ,99},
{30 ,40 ,88}
},
{
{50 ,60 ,77},
{80 ,70 ,66}
},
};
Copy after login

Arrays are inside an array, and hence it is called an array of 2D arrays. If we see it clearly in the above example, there are two 2D arrays of numbers and this 2D.

How to Initialize Elements of 3D Arrays in Java?

As mentioned above, initializing the whole array at once is a best practice when working with 3D arrays as it reduces the chances of confusion for future programming. Though we can also assign one value at a time in an array which can be done in the way mentioned below:

Syntax:

int employee_arr[ ] [ ] [ ] = new int [10][3][3];
employee_arr[0][0][0] = 100; // it will assign value 100 at very first element of employee_arr employee_arr[0][0][1] = 200; // it will assign value 200 at second element of employee_arr employee_arr[0][0][2] = 300; // it will assign value 100 at third element of employee_arr
Copy after login

The above approach is tiresome and not considered to be a good approach as it occupies a lot of space and increases the lines of code. There is also one approach using the loops, which are considered to be a good practice when working with 3D arrays.

Syntax:

int Student_arr [ ] [ ] [ ] = new arr [2] [3] [4]; int x, y, z, value;
for(x = 0; x< 2; x++) {
for(y = 0; y< 3; y++) {
for(z = 0; z< 4; z++) {
Student_arr[x][y][z] = value; value= value*2;
}
}
}
Copy after login

In the above example, all the array elements are inserted using the loops where x = no. of tables, y= total number of rows and z denotes the total number of columns in a 3D array named Student_arr.

How to Access Elements of 3D Arrays in Java?

In Java, though we can access the single element of the array using the indices as we have initialized them by indexes similar to the one given below:

Syntax:

int arr [ ] [ ] [ ] = new arr [3] [3] [3];
// Accessing the array elements of 3D arrays in Java using indices
Copy after login

Syntax:

System.out.println("The first element of array is" + arr[0][0][0]);
Copy after login

In the above syntax, it will retrieve the element at [0][0][0] index of the array ‘arr’, but normally if we want to retrieve all the elements of an array, then this approach is not followed, and elements are accessed through loops as it retrieves all elements at once. While accessing elements through loops, 3 loops are used in which the first loop defines the total number of tables and the second loop defines the rows, and the third loop defines the columns as given below:

Code:

class Student{
public static void main(String[] args) {
// student_arr is the name of 3d array int[][][] student_arr= {
{
{10, 20, 30},
{20, 30, 40}
},
{
{40, 50, 60},
{10, 70, 80},
}
};
// for loop to iterate through each element of 3D array for (tables = 0; tables<2; tables++)
{
for (rows= 0; rows <2; rows++)
{
for (columns= 0; columns<3; columns++)
{
System.out.print("student_arr[" +tables+ "][" +rows+ "][" +columns+ "] = "
+student_arr[tables][rows][columns]+ "\t");
}
System.out.println();
}
System.out.println();
}
}
Copy after login

Output:

student_arr[0] [0] [0] = 10 student_arr[0] [0] [1] = 20 student_arr[0] [0] [2] = 30
student_arr[0] [1] [0] = 20 student_arr[0] [1] [1] = 30 student_arr[0] [1] [2] = 40
student_arr[1] [0] [0] = 40 student_arr[1] [0] [1] = 50 student_arr[1] [0] [2] = 60
student_arr[1] [1] [0] = 10 student_arr[1] [1] [1] = 70 student_arr[1] [1] [2] = 80

How to Remove Elements of 3D Arrays in Java?

  • Removing elements in 3D arrays in Java is simple and similar to the one initializing them. Array class does not provide any direct method to add or delete an element from the arrays. As the size of the array cannot be increased or decreased dynamically so simple programming logic is applied to perform this task. Simply we can use 3 loops to traverse the whole array by specifying the index at which we want to remove the element. We can create a new array or copy the original array leaving the element which needs to be removed.
  • Through this process of removing and updating elements in the 3D array is rarely used. Instead, ArrayList is used in these types of cases as it provides various functions to directly remove elements from it. In ArrayList ‘remove()’ method, remove elements at the provided index in an ArrayList. If we have repeating values in an array and we want to remove the first occurrence in the Array, we can use, ArrayUtils.removeElement(array, element) method for the same, which takes 2 arguments, i.e. the whole array and the element which needs to be removed from it.

How to Update Elements

There is as such no method to update elements in a 3D array. Some programming logic is applied to modify the elements, like removing the elements by traverse the whole array with the use of 3 loops and perform the modification either at the particular index or in the whole array. For such a complex task, this processing is not preferred through 3D arrays and done through the use of the collection, ArrayList. In ArrayList set(int index, E element) is used to modify or update the element dynamically in an array. It takes 2 arguments, i.e. the index and element with the modified and updated value.

Conclusion

As we mentioned above, how to work on 3D arrays in Java. Working with multidimensional arrays in Java is somewhat difficult for the new programmers as it involves various loops, but understanding it through the stepwise procedure and keeping in mind the basic rules while working with arrays can make it much easier to work on it.

The above is the detailed content of 3D Arrays in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template