Java Utility Library

Java Arrays - asList() Method



The java.util.Arrays.asList() method returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs.

Syntax

public static <T> List<T> asList(T...a)

Here, T is the class of the objects in the array.


Parameters

a Specify the array by which the list will be backed.

Return Value

Returns a list view of the specified array.

Exception

NA.

Example:

In the example below, the java.util.Arrays.asList() method returns a fixed-size list backed by the given array.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an array of strings
    String Arr[] = {"Alpha", "Coding", "Skills"};

    //Creating a list 
    List MyList = Arrays.asList(Arr);

    //printing the list
    System.out.print("MyList contains: " + MyList); 
  }
}

The output of the above code will be:

MyList contains: [Alpha, Coding, Skills]

❮ Java.util - Arrays