The asList() metoda java.util.pole class se používá k vrácení seznamu pevné velikosti podporovaného zadaným polem. Tato metoda funguje jako a most mezi API založenými na poli a na kolekcích , v kombinaci s Collection.toArray(). Vrácený seznam je serializovatelný a implementuje RandomAccess.
Spropitné: Toto běží v O(1) čase.
Syntax:
public static List asList(T... a)>
Parametry: Tato metoda trvá pole a který je třeba převést na seznam. Zde … je známý jako vararg což je pole parametrů a funguje podobně jako parametr pole objektů.
Zvláštní poznámka: Typ pole musí být Wrapper Class (Integer, Float atd.) v případě primitivních datových typů (int, float atd.), tj. nemůžete předat int a[], ale můžete předat Integer a[]. Pokud předáte int a[], tato funkce vrátí Seznam a nikoli Seznam , protože v tomto případě nedochází k automatickému boxování a int a[] je sám identifikován jako objekt a místo seznamu je vráceno pole List of int. celých čísel, což způsobí chybu v různých funkcích kolekce.
Návratová hodnota: Tato metoda vrací a zobrazení seznamu zadaného pole.
Příklad 1:
Jáva
// 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);> > }> > }> }> |
>
>
pole struktur v jazyce cVýstup
The list is: [A, B, C, D]>
Příklad 2:
Jáva
// 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);> > }> > }> }> |
>
>Výstup
The list is: [10, 20, 30, 40]>
Příklad 3:
Jáva
// 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);> > }> > }> }> |
>
aws sns
>
Výstup: