Arrays asList() Method in Java with Examples (original) (raw)

Last Updated : 14 Apr, 2026

The Arrays.asList() method in Java is part of the java.util.Arrays class, which is used to convert an array into a fixed-size list.

import java.util.Arrays; import java.util.List;

public class Example { public static void main(String[] args) {

    // Creating an array of Integer type
    Integer[] a = {1, 2, 3, 4, 5};

    // Getting the list view of the array
    List<Integer> l = Arrays.asList(a);
    
    // Printing the list
    System.out.println("" + l);
    
    // A change made in the array would also
    // reflect in the list    
    a[2] = 20;
    System.out.println("" + l.get(2));
}

}

`

Syntax

public static List asList(T... a)

**Parameters:

**Return Value: List<T>: This method returns a fixed-size list that contains elements of the same type as the array elements.

Examples of Using the asList() Method in Java

**Example: Using asList() Method with String Array

Java `

import java.util.*;

public class GFG {

public static void main(String[] args)
{

    // Creating array of String type
    String[] a = { "A", "B", "C", "D" };

    // Getting the list view of the array
    List<String> l = Arrays.asList(a);

    // Printing all the elements in the list
    System.out.println(l);
}

}

`

**Example: Using asList() Method with Integer Array

Java `

import java.util.*;

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<Integer> l = Arrays.asList(a);

        // Printing all the elements inside list object
        System.out.println("" + l);
    }

    // Catch block to handle exceptions
    catch (NullPointerException e) {

        System.out.println("Exception thrown: " + e);
    }
}

}

`

**Example: UnsupportedOperationException with asList()

Java `

import java.util.*;

public class GFG {

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<Integer> l = Arrays.asList(a);

        // Adding another int to the list
        // As Arrays.asList() returns fixed size
        // list, we'll get
        // java.lang.UnsupportedOperationException
        l.add(50);

        // Printing all the elements of list
        System.out.println("" + l);
    }

    // Catch block to handle exceptions
    catch (UnsupportedOperationException e) {

        // Display message when exception occurs
        System.out.println("Exception thrown: " + e);
    }
}

}

`

Output

Exception thrown: java.lang.UnsupportedOperationException

**Note: The code compiles successfully, but modifying the list (using add() or remove()) throws a UnsupportedOperationException at runtime because the list is fixed-size

**Output:

Output