Arrays asList() Method in Java with Examples (original) (raw)
Last Updated : 25 Nov, 2024
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. This method acts as a bridge between array-based and collection-based APIs, in combination with **Collection.toArray().
**Example:
Below is a simple example of the Arrays asList()
method
to convert an array of integers (using the Integer wrapper class) into a list.
Java `
import java.util.Arrays; import java.util.List;
public class Example { public static void main(String[] args) {
// Creating an array of Integer type
Integer[] n = {1, 2, 3, 4, 5};
// Getting the list view of the array
List<Integer> l = Arrays.asList(n);
System.out.println("" + l);
}
}
`
Table of Content
**Syntax of asList()
public static List asList(T... a)
**Parameters:
- "T... a****"** is an array of elements to be converted into a List.
- The "
..."
indicates that this is a varargs parameter and it allows multiple elements of type**T
** to be passed as an array.
**Important Points: The array type must be a wrapper class i.e.
Integer
,Float,
for primitive data types i.e.int
,float.
You cannot passint[]
, but you can passInteger[]
. Passingint[]
will return aList<int[]>
instead of aList<Integer>
, as autoboxing does not occur in this case, which may cause errors in collection functions.
**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
Using asList()
Method with String Array
The below example demonstrates how to convert a String array into a fixed-size list using the asList()
method.
Java `
// Java Program to Demonstrate asList() method // of Arrays class for a string value 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 String type
String a[]
= new String[] { "A", "B", "C", "D" };
// Getting the list view of Array
List<String> l = Arrays.asList(a);
// Printing all the elements in list object
System.out.println("" + l);
}
// Catch block to handle exceptions
catch (NullPointerException e) {
System.out.println("Exception thrown: " + e);
}
}
}
`
Using asList() Method with Integer Array
The below example demonstrates how to convert an **Integer array into a fixed-size list using the asList()
method.
Java `
// Java program to Demonstrate asList() method // of Arrays class For an integer value 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);
}
}
}
`
UnsupportedOperationException with asList()
In this example, we will see that trying to change the list from asList()
will cause an **UnsupportedOperationException.
Java `
// Java Program to demonstrate asList() method // Which returns fixed size list and // throws UnsupportedOperationException // if any element is added using add() method 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
**Output: