Java Arrays asList() Method



Description

The Java Arrays asList(T... a) 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.

Declaration

Following is the declaration for java.util.Arrays.asList() method

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

Parameters

a − This is the array by which the list will be backed.

Return Value

This method returns a list view of the specified array.

Exception

NA

Getting List of String from Array Example

The following example shows the usage of Java Arrays asList() method. First, we've created an array of Strings and then used asList() method to create a corresponding list and then the list is printed.

package com.tutorialspoint;
import java.util.Arrays;
import java.util.List;
public class ArrayDemo {
   public static void main (String args[]) {

      // create an array of strings
      String a[] = new String[]{"abc","klm","xyz","pqr"};

      List<String> list = Arrays.asList(a);

      // printing the list
      System.out.println("The list is:" + list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The list is:[abc, klm, xyz, pqr]

Getting List of Integers from Array Example

The following example shows the usage of Java Arrays asList() method. First, we've created an array of Integer and then used asList() method to create a corresponding list and then the list is printed.

package com.tutorialspoint;
import java.util.Arrays;
import java.util.List;
public class ArrayDemo {
   public static void main (String args[]) {

      // create an array of integers
      Integer a[] = new Integer[]{1, 2, 3, 4};

      List<Integer> list = Arrays.asList(a);

      // printing the list
      System.out.println("The list is:" + list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The list is:[1, 2, 3, 4]

Getting List of Objects from Array Example

The following example shows the usage of Java Arrays asList() method. First, we've created an array of Student objects and then used asList() method to create a corresponding list and then the list is printed.

package com.tutorialspoint;
import java.util.Arrays;
import java.util.List;
public class ArrayDemo {
   public static void main (String args[]) {

      // create an array of students
      Student a[] = new Student[]{new Student(1, "Julie"), new Student(2, "Robert"), 
         new Student(3, "Adam")};
      List<Student> list = Arrays.asList(a);

      // printing the list
      System.out.println("The list is:" + list);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The list is:[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arrays.htm
Advertisements