- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we convert a Java array to list?
We can use Arrays.asList() method to convert a Java array to List easily.
Syntax
public static <T> List<T> 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, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
Type Parameter
T − The runtime type of the array.
Parameters
a − The array by which the list will be backed.
Returns
A list view of the specified array.
Example
The following example shows how to get immutable and mutable list using Arrays.asList() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { Integer[] array = {1,2,3,4,5,6}; // Get a mutable list from array List<Integer> list = new ArrayList<>(Arrays.asList(array)); list.add(7); System.out.println("List: " + list); // Get immutable list from array List<Integer> list1 = Arrays.asList(array); try { list1.add(7); } catch(Exception e) { e.printStackTrace(); } System.out.println("List: " + list1); } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5, 6, 7] List: [1, 2, 3, 4, 5, 6] java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:19)
- Related Articles
- Can we convert a Java list to array?
- Can we convert a list to an Array in Java?
- How can we convert a list to the JSON array in Java?
- Can we convert an array to list and back in Java?
- Can we convert a list to a Set in Java?
- How can we convert a JSON array to a list using Jackson in Java?\n
- How can we convert list to Set in Java?
- Can we convert a List to Set and back in Java?
- How can we convert a JSONArray to String Array in Java?
- How can we convert character array to a Reader in Java?
- convert list to array in java
- Java program to convert a list to an array
- Java program to convert an array to a list
- How to convert a list to array in Java?
- In how many ways we can convert a String to a character array using Java?

Advertisements