首頁  >  文章  >  Java  >  Java 中的 3D 數組

Java 中的 3D 數組

WBOY
WBOY原創
2024-08-30 15:27:261206瀏覽

在了解 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