Java の配列は、C/C++ の配列と比較すると実装と使用法が異なりますが、多くの類似点もあります。ここでは、Java で配列を返す方法について説明します。
Java で配列を返すには、次の点に注意する必要があります。
キーポイント 1: 配列を返すメソッドの戻り型は、返される配列と同じデータ型の配列である必要があります。戻り値の型は、通常の Integer、Double、Character、String、またはユーザー定義のクラス オブジェクトにすることもできます。
// Method returning an integer array. int[] methodName() {...}> // Method returning a String array. String[] methodName() {...}> // Method returning an array of objects of class named Students. Students[] methodName() {...}> キーポイント 2: アクセス修飾子は、メソッドと返される配列の使用法を考慮して正確に使用する必要があります。静的宣言と非静的宣言も考慮する必要があります。
// Using public access modifier and static to call the method from a static class, method or block. public static char[] methodName() {...}> キーポイント 3: 返される配列を処理するには、メソッド呼び出し時に同じデータ型または同様の変数配列が存在する必要があります。たとえば、任意のメソッドから返された整数配列は次のように格納できます。
int[] storage = methodReturningArray();>
実装:
これをよりよく理解するために、配列を返す可能性のあるいくつかの異なる種類のシナリオを検討してみましょう。ここでは 3 つのシナリオの例を検討します。
- ケース 1: 単純な組み込み配列
- ケース 2: オブジェクトの配列
- ケース 3: 多次元配列を返す
ケース 1: Javaで整数(組み込みデータ型)配列を返す
forループJava
組み込みデータ型の配列は、整数、文字、浮動小数点、倍精度のいずれであっても、上記の点に留意して return ステートメントを使用するだけですべて返すことができます。
例
ジャワ
// Java Program to Illustrate Returning> // simple built-in arrays> // Importing input output classes> import> java.io.*;> // Main class> class> GFG {> >// Method 1> >// Main driver method> >public> static> void> main(String[] args)> >{> >// An integer array storing the returned array> >// from the method> >int>[] storage = methodReturningArray();> >// Printing the elements of the array> >for> (>int> i =>0>; i System.out.print(storage[i] + ' '); } // Method 2 // Returning an integer array public static int[] methodReturningArray() { int[] sample = { 1, 2, 3, 4 }; // Return statement of the method. return sample; } }> |
>
>出力
1 2 3 4>
ケース 2: Javaでオブジェクトの配列を返す
これは、組み込みデータ型の配列を返す場合とまったく同様の方法で行われます。
例
ジャワ
// Java Program to Illustrate Returning> // an array of objects in java> // Importing all input output classes> import> java.io.*;> // Class 1> // Helper class> // Courses whose objects are returned as an array> class> Courses {> >String name;> >int> modules;> >// Constructor to instantiate class objects.> >public> Courses(String n,>int> m)> >{> >// This keyword refers to current instance itself> >this>.name = n;> >this>.modules = m;> >}> }> // Class 2> // Main class> class> GFG {> >// Method 1> >// Main driver method> >public> static> void> main(String[] args)> >{> >// Calling the method for returning an array of> >// objects of the Courses class.> >Courses[] sample = methodReturningArray();> >// Printing the returned array elements.> >for> (>int> i =>0>; i System.out.print(sample[i].name + ' - ' + sample[i].modules + ' modules
'); } // Method 2 // Note that return type is an array public static Courses[] methodReturningArray() { // Declaring Array of objects of the Courses class Courses[] arr = new Courses[4]; // Custom array of objects arr[0] = new Courses('Java', 31); arr[1] = new Courses('C++', 26); arr[2] = new Courses('DSA', 24); arr[3] = new Courses('DBMS', 12); // Statement to return an array of objects return arr; } }> |
>
>出力
Java - 31 modules C++ - 26 modules DSA - 24 modules DBMS - 12 modules>
ケース 3: 多次元配列を返す
Javaの多次元配列 は配列内の配列の配列であると言えます。最も単純な形式は 2 次元配列です。サイズとそのサイズに応じた宣言があります。ここでは、1 次元配列と非常によく似たアプローチによる 2 次元配列の返しを以下に示します。
例
ジャワ
// Java Program to Illustrate Returning> // Multi-dimensional Arrays> // Importing input output classes> import> java.io.*;> // Main class> class> GFG {> >// Method 1> >// Main driver method> >public> static> void> main(String[] args)> >{> >// An integer 2D array storing the> >// returned array from the method> >int>[][] storage = methodReturningArray();> >// Printing the elements of the array> >// using nested for loops> >for> (>int> i =>0>; i for (int j = 0; j 0].length; j++) System.out.print(storage[i][j] + ' '); System.out.println(); } } // Method 2 // Returning an integer array public static int[][] methodReturningArray() { // Custom 2D integer array int[][] sample = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Return statement of the method return sample; } }> |
>
>出力
1 2 3 4 5 6 7 8 9>