Java の 3D 配列

WBOY
リリース: 2024-08-30 15:27:26
オリジナル
1203 人が閲覧しました

Java の 3D 配列を理解する前に、配列とは何なのか、またそれがプログラミング言語でなぜ使用されるのかを知る必要があります。配列は基本的に、同じ名前で参照される同様のタイプの値のグループです。類似した型とは、同じデータ型の値を意味します。クラスの生徒全員の名前を保存したい状況を考えてみましょう。 Student の名前は String データ型であるため、各生徒の名前を別の変数に格納することは、多くのスペースを占有するだけでなく、ほぼ同じ変数を増やすことによってプログラム内で混乱を引き起こすため、正しくありません。コード行。したがって、このような種類の状況に対処するには、配列が使用されます。プログラマは Student_name の配列を作成し、配列オブジェクトの作成時にそのサイズを指定できます。この方法では、各生徒の名前に変数名を指定する必要がなくなり、値を更新、挿入、取得するときはいつでも、この配列のインデックスを使用できます。

Java では、配列変数は他の変数と同様に、データ型の後に [] 記号を付けて宣言されます。配列のサイズは配列の作成時に定義する必要があり、それは一定のままです。配列要素は数値インデックスによってアクセスされ、最初の要素は 0 インデックスに格納されます。 Java には基本的に 2 種類の配列、つまり 1 次元配列と多次元配列があります。 3D 配列は、多次元配列のカテゴリに分類されます。簡単に言えば、多次元配列は配列の配列として定義でき、3D 配列は 2D 配列の配列です。 3D は、多次元配列の複雑な形式です。建物内のアパートのシナリオを考えてみましょう。アパートには 10 階建てで、各階には 5 つのアパートがあり、各アパートには 3 つの部屋があるとします。プログラミングでこのデータを処理するには、3D 配列が使用されます。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

Java で 3D 配列はどのように定義されますか?

Java は非常に簡単な方法で配列を定義します。角括弧 (「[ ]」) は、配列のデータ型の後に配列オブジェクトを定義するために使用されます。配列の宣言時にサイズを定義する必要があります。 3D 配列は 3 つの括弧で定義されます。以下に、Java で 3D 配列を定義する構文を示します。

Data_type array_name[ ] [ ] [ ] = new array_name[a][b][c];
ログイン後にコピー
  • ここで data_type: 配列に格納される要素のデータ型。 array_name: 配列の名前
  • 新しい: Java でオブジェクトを作成するためのキーワード
  • a、b、c: さまざまな次元の数値を保持します。

構文:

int [ ] [ ] [ ] arr = new int [10][4][3];
ログイン後にコピー

上記の例では、配列「arr」には最大 10x4x3 = 120 個の要素を格納できます。

Java で 3D 配列を作成し、そこに値を挿入する方法?

Java での 3D 配列の作成は、1D 配列や 2D 配列を作成するのと同じくらい簡単です。上で述べたように、配列のサイズを宣言時に定義することが重要です。 3D 配列の作成には、2D 配列の配列の形式で値を渡したり入力したりするもう 1 つのステップが必要です。配列のサイズを定義し、後で値を挿入/入力することも、配列内の値を直接渡すこともできます。したがって、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 配列の配列と呼ばれます。上の例ではっきりとわかるように、数値の 2 次元配列とこの 2 次元が 2 つあります。

Java で 3D 配列の要素を初期化する方法?

上で述べたように、3D 配列を扱う場合は、配列全体を一度に初期化することが、将来のプログラミングで混乱が生じる可能性を減らすためのベスト プラクティスです。ただし、以下で説明する方法で配列に一度に 1 つの値を割り当てることもできます。

構文:

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
ログイン後にコピー

上記のアプローチは面倒であり、多くのスペースを占有し、コード行が増えるため、良いアプローチとは考えられていません。ループを使用するアプローチも 1 つあり、これは 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!