の asList() の方法 java.util.Arrays クラスは、指定された配列に基づく固定サイズのリストを返すために使用されます。このメソッドは次のように機能します。 配列ベースの API とコレクションベースの API の間のブリッジ 、Collection.toArray() と組み合わせて使用します。返されたリストはシリアル化可能であり、RandomAccess を実装します。
ヒント: これは O(1) 時間で実行されます。
構文:
public static List asList(T... a)>
パラメーター: このメソッドは、 配列a これはリストに変換する必要があります。ここでは…として知られています 可変長 これはパラメータの配列であり、オブジェクト配列パラメータと同様に機能します。
特記事項: プリミティブ データ型 (int、float など) の場合、配列の型はラッパー クラス (Integer、Float など) である必要があります。つまり、 int a[] を渡すことはできませんが、Integer a[] は渡すことができます。 int a[] を渡すと、この関数は List ではなく List を返します。この場合オートボクシングは発生せず、 int a[] 自体がオブジェクトとして識別され、リストの代わりに int 配列の List が返されるためです。整数の場合、さまざまなコレクション関数でエラーが発生します。
戻り値: このメソッドは、 リストビュー 指定された配列の。
例 1:
ジャワ
// Java program to Demonstrate asList() method> // of Arrays class for a string value> // Importing utility classes> import> java.util.*;> // Main class> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] argv)>throws> Exception> >{> >// Try block to check for exceptions> >try> {> >// Creating Arrays of String type> >String a[]> >=>new> String[] {>'A'>,>'B'>,>'C'>,>'D'> };> >// Getting the list view of Array> >List list = Arrays.asList(a);> >// Printing all the elements in list object> >System.out.println(>'The list is: '> + list);> >}> >// Catch block to handle exceptions> >catch> (NullPointerException e) {> >// Print statement> >System.out.println(>'Exception thrown : '> + e);> >}> >}> }> |
>
>
Javaの部分文字列の例出力
The list is: [A, B, C, D]>
例 2:
ジャワ
// Java program to Demonstrate asList() method> // of Arrays class For an integer value> // Importing utility classes> import> java.util.*;> // Main class> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] argv)>throws> Exception> >{> >// Try block to check for exceptions> >try> {> >// Creating Arrays of Integer type> >Integer a[] =>new> Integer[] {>10>,>20>,>30>,>40> };> >// Getting the list view of Array> >List list = Arrays.asList(a);> >// Printing all the elements inside list object> >System.out.println(>'The list is: '> + list);> >}> >// Catch block to handle exceptions> >catch> (NullPointerException e) {> >// Print statements> >System.out.println(>'Exception thrown : '> + e);> >}> >}> }> |
>
>出力
The list is: [10, 20, 30, 40]>
例 3:
ジャワ
// Java Program to demonstrate asList() method> // Which returns fixed size list and> // throws UnsupportedOperationException> // if any element is added using add() method> // Importing required classes> import> java.util.*;> // Main class> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] argv)>throws> Exception> >{> >// Try block to check for exceptions> >try> {> >// Creating Arrays of Integer type> >Integer a[] =>new> Integer[] {>10>,>20>,>30>,>40> };> >// Getting the list view of Array> >List list = Arrays.asList(a);> >// Adding another int to the list> >// As Arrays.asList() returns fixed size> >// list, we'll get> >// java.lang.UnsupportedOperationException> >list.add(>50>);> >// Printing all the elements of list> >System.out.println(>'The list is: '> + list);> >}> >// Catch block to handle exceptions> >catch> (UnsupportedOperationException e) {> >// Display message when exception occurs> >System.out.println(>'Exception thrown : '> + e);> >}> >}> }> |
>
>
出力:
