The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array.
This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
Syntax:
public static List asList(T... a)
Parameters: This method takes the array a which is required to be converted into a List.
Return Value: This method returns a list view of the specified array.
Below are the examples to illustrate the asList() method.
Example 1:
// Java program to demonstrate // asList() method for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating Arrays of String type String a[] = new String[] { "A", "B", "C", "D" }; // getting the list view of Array List<String> list = Arrays.asList(a); // printing the list System.out.println("The list is: " + list); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
The list is: [A, B, C, D]
Example 2:
// Java program to demonstrate // asList() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating Arrays of Integer type Integer a[] = new Integer[] { 10, 20, 30, 40 }; // getting the list view of Array List<Integer> list = Arrays.asList(a); // printing the list System.out.println("The list is: " + list); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
The list is: [10, 20, 30, 40]
Recommended Posts:
- Java 8 | Arrays parallelSort() method with Examples
- Ints asList() function | Guava | Java
- Java.util.Arrays.equals() in Java with Examples
- Arrays.binarySearch() in Java with examples | Set 1
- Arrays copyOf() in Java with examples
- Arrays.sort() in Java with examples
- Arrays.fill() in Java with Examples
- Arrays.toString() in Java with Examples
- Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray)
- util.Arrays vs reflect.Array in Java with Examples
- Arrays stream() method in Java
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



