首页 >Java >java教程 >Java 中的 3D 数组

Java 中的 3D 数组

WBOY
WBOY原创
2024-08-30 15:27:261251浏览

在了解 Java 中的 3D 数组之前,我们应该知道数组是什么以及为什么在编程语言中使用它?数组基本上是一组由相同名称引用的相似类型的值。相似类型是指相同数据类型的值。考虑这样一种情况,我们想要存储班级所有学生的姓名。由于Student的名字是String数据类型,但是将每个学生的名字存储在不同的变量中是不正确的,因为它不仅会占用大量空间,而且还会因为增加几乎相同的值而在程序中造成混乱代码行。因此,为了处理这些类型的情况,需要使用数组。程序员可以创建一个 Student_names 数组,并在创建数组对象时指定其大小。这样,就不需要为每个学生姓名指定变量名称,并且每当我们想要更新、插入和检索值时,都可以使用该数组的索引。

在Java中,数组变量的声明方式与其他变量类似,在其数据类型后面带有[]符号。数组大小需要在创建数组时定义,并且保持不变。数组元素通过数字索引访问,第一个元素存储在 0 索引处。 Java 中的数组基本上有两种类型,即一维数组和多维数组。 3D 数组属于多维数组类别。多维数组,简单来说,可以定义为数组的数组,3D数组是2D数组的数组。 3D 是多维数组的复杂形式。考虑建筑物中公寓的场景。假设公寓有10层,每层有5个单位,每个单位有3个房间。为了在编程中处理这些数据,使用了 3D 数组。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

Java 中如何定义 3D 数组?

Java 使用一种非常简单的方式来定义数组。方括号(‘[ ]’)用于在数组的数据类型之后定义数组对象。需要在声明数组时定义其大小。 3D 数组由三个括号定义。下面给出了在 Java 中定义 3D 数组的语法:

Data_type array_name[ ] [ ] [ ] = new array_name[a][b][c];
  • 这里的data_type:将存储在数组中的元素的数据类型。 array_name: 数组的名称
  • new:Java 中创建对象的关键字
  • a、b、c:保存各个维度的数值。

语法:

int [ ] [ ] [ ] arr = new int [10][4][3];

上例中数组‘arr’最多可以存储 10x4x3 = 120 个元素。

如何在 Java 中创建 3D 数组并在其中插入值?

在 Java 中创建 3D 数组就像创建 1D 和 2D 数组一样简单。如上所述,在声明时定义数组的大小非常重要。创建 3D 数组还涉及以 2D 数组的数组形式在其中传递/输入值的步骤。我们可以定义数组的大小,然后可以插入/输入值,或者我们可以直接在数组中传递值。因此,3D 数组中定义值的方式如下:

语法

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

代码

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

数组位于数组内部,因此称为二维数组的数组。如果我们在上面的例子中清楚地看到,有两个 2D 数字数组和这个 2D。

如何在 Java 中初始化 3D 数组的元素?

如上所述,在使用 3D 数组时,一次初始化整个数组是最佳实践,因为它可以减少未来编程中出现混乱的机会。虽然我们也可以在数组中一次分配一个值,这可以通过下面提到的方式完成:

语法:

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

上面的方法很烦人,不被认为是一个好的方法,因为它占用了大量的空间并增加了代码行数。还有一种使用循环的方法,这被认为是处理 3D 数组时的良好实践。

语法:

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;
}
}
}

在上面的示例中,所有数组元素都是使用 x = no 的循环插入的。表的数量,y= 总行数,z 表示名为 Student_arr 的 3D 数组中的总列数。

如何在 Java 中访问 3D 数组的元素?

在 Java 中,尽管我们可以使用索引访问数组的单个元素,因为我们已经通过类似于下面给出的索引初始化了它们:

语法:

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

语法:

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

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();
}
}

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.

以上是Java 中的 3D 数组的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn