The asList() metoda java.util.Arrays se uporablja za vrnitev seznama fiksne velikosti, podprtega s podano matriko. Ta metoda deluje kot a most med API-ji, ki temeljijo na nizih in zbirkah , v kombinaciji z Collection.toArray(). Vrnjeni seznam je mogoče serializirati in izvaja RandomAccess.
Nasvet: To se izvaja v O(1) času.
Sintaksa:
public static List asList(T... a)>
Parametri: Ta metoda zajema niz a ki ga je treba pretvoriti v seznam. Tukaj ... je znan kot vararg ki je niz parametrov in deluje podobno kot parameter niza objektov.
Posebna opomba: Tip matrike mora biti Wrapper Class (Integer, Float itd.) v primeru primitivnih tipov podatkov (int, float itd.), tj. ne morete posredovati int a [], lahko pa posredujete Integer a []. Če podate int a[], bo ta funkcija vrnila seznam in ne seznama, saj se v tem primeru ne zgodi samodejno pakiranje in je int a[] sam identificiran kot objekt in vrnjen je seznam matrike int namesto seznama celih števil, kar bo povzročilo napako v različnih funkcijah zbiranja.
Povratna vrednost: Ta metoda vrne a pogled seznama navedenega niza.
strani, kot je bedpage
Primer 1:
Java
razlika med tigrom in levom
// 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);> > }> > }> }> |
>
>Izhod
kaj je modulo v c++
The list is: [A, B, C, D]>
Primer 2:
Java
// 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);> > }> > }> }> |
>
>Izhod
The list is: [10, 20, 30, 40]>
Primer 3:
Java
predmet matrike v Javi
terminal kali linux
// 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);> > }> > }> }> |
>
>
Izhod: