Table of contents
1.
Introduction
2.
What are arrays asList () in Java?
3.
Syntax of Arrays asList() in Java
4.
Parameters of Arrays asList() in Java
5.
Return Value of Arrays asList() in Java
6.
Examples of Java Arrays asList Method
6.1.
Java
6.2.
Java
6.3.
Java
6.4.
Java
6.5.
Java
7.
Frequently Asked Questions
7.1.
What does array asList () contain?
7.2.
What is the difference between ArrayList and array asList in Java?
7.3.
What is the purpose of asList?
7.4.
What is the use of java arrays asList function if it returns only fixed length list?
7.5.
How can the Java arrays asList method be used to initialize a fixed-size list?
7.6.
What does array asList() do?
8.
Conclusion
Last Updated: Oct 7, 2024
Easy

Arrays asList() in Java

Author Nikhil Joshi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the Arrays.asList() method is used to convert an array into a fixed-size List. It takes an array as input and returns a List view of the array, allowing you to manipulate the array elements using List methods. However, the returned List is backed by the original array, so any changes made to the List will reflect in the array and vice versa. In this article, we will discuss this in detail where we will talk about syntax, parameters, and examples.

Java Arrays asList

 

What are arrays asList () in Java?

The Java arrays asList method bridges the old array-based API and the new Collections API. The new collection API consists of several functions that can improve performance and reduce the program time. We must convert our array to the ArrayList to use this collection API.

Also see, Swap Function in Java

Syntax of Arrays asList() in Java

Following is the syntax of Java arrays asList method:

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

 

You can pass an array of any type (T), and the method will return a List<T> containing the elements of that array.

Parameters of Arrays asList() in Java

In Java, the Arrays.asList() method accepts one parameter, which is the array you want to convert into a List.

Return Value of Arrays asList() in Java

The Java Arrays.asList() method returns a List view of the provided array. 

Examples of Java Arrays asList Method

Code 1. asList method does not support primitive data types. We need to use wrapper classes. The following code demonstrates the fact:

  • Java

Java

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


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       int[] arr = {1,2,34,56,78,89};
      
       // Trying to create a list using Java arrays asList
       List<Integer> list = Arrays.asList(arr);
       printList(list);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i) );
       }
       System.out.println();
   }
}
You can also try this code with Online Java Compiler
Run Code


This code will give a compilation error because the java arrays asList method does not support primitive data types.

Output

/home/nickiosi/IdeaProjects/javacp/src/AsListExamples.java: 10:43
java: incompatible types: inference variable T has incompatible bounds
      equality constraints: java. lang. Integer
      Lower bounds: int[]

 

Corrected Code

  • Java

Java

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


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);
       printList(list);
   }


   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}
You can also try this code with Online Java Compiler
Run Code


Output

1 2 34 56 78 89
Process finished with exit code 0

Instead of using primitive data types like int, char, long etc., use wrapper classes like Integer, Character, Long etc.

 

Code 3. The list returned by the java arrays asList method is of fixed size. It is not possible to add or remove items from that list. The following code demonstrates the fact:

  • Java

Java

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


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);


       // Trying to perform add and remove function on the list
       list.add(24);
       list.remove(2);


       printList(list);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}
You can also try this code with Online Java Compiler
Run Code


This code will result in a runtime error because the list returned by the java arrays asList method is of fixed size. It is not possible to add or remove items from the list.

Output

/home/nickjosi/IdeaProjects/javacp/src/AsListExamples.java:10:43
java: incompatible types: inference variable E has incompatible bounds
equality constraints: java.lang.Integer
lower bounds: int[]

 

Code 4set(index,newValue) function can be used to change the value of a particular element of the returned list. The following code demonstrates the fact: 

  • Java

Java

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


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);


       System.out.println("List before performing set operation");
       printList(list);
       System.out.println("2nd index element before set operation");
       System.out.println(arr[2]);


       // Trying to perform set function on the list
       list.set(2,101);
      
       System.out.println("List after performing set operation");
       printList(list);
       System.out.println("2nd index element after set operation");
       System.out.println(arr[2]);


       System.out.println("Change in the list is also reflected in the array");
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}
You can also try this code with Online Java Compiler
Run Code


The returned list is of fixed length, but the set function can change its content. We can transform an element at a specified index using the set function.

Output:

/home/nickjosi/IdeaProjects/javacp/src/AsListExamples.java:10:43
java: incompatible types: inference variable E has incompatible bounds
equality constraints: java.lang.Integer
lower bounds: int[]

But what if our application demands adding or removing items from the returned list? A simple solution is to create a new list using ArrayList(Collection<? extends E> c) constructor.

 

Code 5. If you don’t want a fixed-size list, the simple solution is to create a different copy list of the returned list. The following code demonstrates the fact:

  • Java

Java

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


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);
       List<Integer> newList = new ArrayList<>(list);


       // Trying to perform add and remove function on the new list
       newList.add(24);
       newList.remove(2);


       printList(newList);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

1 2 56 78 89 24
Process finished with exit code 0

Frequently Asked Questions

What does array asList () contain?

In Java, the asList() method is typically used to convert an array into a List. It returns a fixed-size List view of the specified array. 

What is the difference between ArrayList and array asList in Java?

ArrayList is a dynamic, resizable collection in Java, while Arrays.asList() returns a fixed-size List view of an array, making it immutable in size.

What is the purpose of asList?

The purpose of asList() in Java is to convert an array into a fixed-size List, providing a convenient way to use List-based operations on Arrays.

What is the use of java arrays asList function if it returns only fixed length list?

Fixed length can be helpful in many applications. It gives us more robust security since any list changes (addition/removal) are impossible. We may also want to convert the fixed-length array to the list to take advantage of Collection API in Java.

How can the Java arrays asList method be used to initialize a fixed-size list?

We can create a fixed-size list using the asList method by the following syntax.

List<String> batsman = Arrays.asList("Sachin", "Virat", "Rohit");

What does array asList() do?

The Arrays.asList() method in Java converts an array into a fixed-size list backed by the original array. Any changes to the list directly affect the array, and vice versa. It is useful for quick list operations without manual array conversion.

Conclusion

In conclusion, the Java arrays asList method bridges the old array API and new collection API. It returns a fixed-size list. We can not add or remove elements from the list, but a particular element can be changed using the set function. Apart from that, It only takes an array of the object. So, if we wish to use a primitive data type array, we need to use the wrapper class. The asList function can be used to take advantage of Collection API.

Recommended Article:

Refer to our guided paths on Code 360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass