Java Arrays.asList Overview
This method is a function of the java utility class Arrays. The java Arrays.asList
function returns a fixed-size list that contains a java Array. It acts like a list wrapped around an array, it provides a list view to an array. This method takes the time complexity of O(1). it runs O(1) times to return a fixed-size list that has the size of the array passed to it. The method does not copy the Array into a list, it makes a list with the elements of the Array, no copies are made.
Class used for java Arrays.asList
The class that is used to perform the action of wrapping a list around an array in java.util.Arrays. The class hierarchy goes something like this
Declaration of Arrays.asList
This method is declared using the syntax as follows
List<datatype> ListName=Arrays.asList(argument)
The argument passed to the function is the array that needs to be wrapped around by a list. This function is a connection function between java Arrays and collection-based APIs.
Different methods to use java Arrays.asList
There are two ways to declare a list using the java Arrays asList method
Method 1: Declare an Array then use Java Arrays.asList
The first method is to declare an array and then use the java Arrays.asList method. Explained in the code below
// class Arrays used to get the method java Arrays.asList
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a string array
String array[]= {"java","asList()"};
// create a new list, and pass the string array to it as a parameter
// use the method java Arrays.asList
List<String>newList=Arrays.asList(array);
//display the list
System.out.print("List has: " + newList);
}
}
The output of the following code would be :
MyList contains: [java, asList()]
Method 2: Declare Array within Java Arrays.asList
Another method can be passing the array elements as an argument to the java Arrays.asList method.
The further explanation can be seen by the code below
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a new list
// pass an array without declaring it to the java Arrays.asList
List<String>newList=Arrays.asList("java","Arrays","asList()");
// display the list
System.out.print("List has: " + newList);
}
}
The output of the following code would be
List has: [java, Arrays , asList]
As you can see both the methods declare the same output, it is optional whether which method can be used to pass the array in the java Arrays.asList() method.
Setting another array to the old list
In the java Arrays.asList()
method, if another array is set in the old list, the updated list will have the latest array passed to it. For example, in the code below two arrays are declared, first, the list is set to the first array, and then the second, the list will display the second array elements only.
Code
// Arrays class imported
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a new list
// pass an array without declaring it to the java Arrays.asList Method
List<String>newList=Arrays.asList("java","Arrays","asList()");
// display the list
newList=Arrays.asList("linux","cloud","tutorial");
System.out.print("List has: " + newList);
}
}
The output of the following code will be
The list has: [linux, cloud, tutorial]
As you can see, the output contains the list of elements of the Array that was declared after the first declaration, hence the latest Array is in the list, the elements of the last Array are discarded.
Change in source Array
If the source array is changed, that change will also reflect in the list that is initialized using java Arrays.asList
function.
// Arrays class imported
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a string array
String[] array = { "java", "Arrays", "asList()" };
// create a new list
// pass an array to the java Arrays.asList() method
List<String> newList = Arrays.asList(array);
// print list before changing array element
System.out.print("List before: " + newList);
// change in the source array
array[0] = "changed index";
// change list after changing array element
System.out.print("\r\nList now: " + newList);
}
}
The output of the following code will be :
List before: [java, Arrays, asList()]
List now: [changed index, Arrays, asList()]
As you can see the string at the 0th index is changed from "java" to "changed index" and the output can be seen clearly by the code.
Change element of the list without changing the array
If we want to change an element within the list we can use the list.set(int index, element to be changed) method.
Code
// Arrays class imported
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a string array
String[] array = { "java", "Arrays", "asList()" };
// create a new list
// pass an array to the java asList() method
List<String> newList = Arrays.asList(array);
//print list before changing array element
System.out.print("List before: " + newList);
// use list.set method
newList.set(1, null);
//change list after changing array element
System.out.print("\r\nList now: " + newList);
}
}
The output of the following code will be:
List before: [java, Arrays, asList()]
List now: [java, null, asList()]
As you can see the element at the second index is changed to null after we used the set function to change its element from "Arrays" to null.
Fixed-size list
The java Array asList method creates a fixed-size list, and the size of that list is the size of the array that the list is wrapped around if we try to add an element to that list, an exception is thrown.
Code
// Arrays class imported
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a string array
String[] array = { "java", "Arrays", "asList()" };
// create a new list
// pass an array to the java asList() method
List<String> newList = Arrays.asList(array);
//print list before changing array element
System.out.print("List before: " + newList);
// use list.set method
newList.add("fourth element");
// change list after changing array element
System.out.print("\r\nList now: " + newList);
}
}
Output
List before: [java, Arrays, asList()]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at main.main(main.java:16)
At the point where we tried to add an element to the list an exception is thrown no further addition can be made, the list has a fixed size now.
Practice code
Here is a code for practice, first determine its output on a paper by dry run method and then copy-paste the code in your desired ide i.e. Eclipse, IntelliJ, NetBeans, or any other online compiler you want to check its output, at what point can there be an exception thrown.
// Arrays class imported
import java.util.Arrays;
import java.util.*;
public class main {
public static void main(String[] args) {
// create a string array
String[] array = { "java", "Arrays", "asList()" };
String []array2= {"linux","cloud","tutorial"};
// create a new list
// pass an array to the java asList() method
List<String> newList = Arrays.asList(array);
newList=Arrays.asList(null);
// print list before changing array element
System.out.print("List before: " + newList);
newList.add("next element");
// change list after changing array element
System.out.print("\r\nList now: " + newList);
}
}
There will be an exception in the code. But at what line?
Conclusion
In this article we have read about the java Arrays asList method, which can be handy while declaring lists and is used to wrap an array within a list, the list has a fixed size, array elements can be changed by either changing array indexes or using the list set method, furthermore if we try to add an element to the fixed size list, an exception will be thrown. This method is used as a bridge between java Arrays and java List collections.
Further reading
To further understand the concept of java Arrays.asList
and other Arrays functions check the following articles
Java Arrays.asList
Java Arrays