Array to List in Java: 5 Ways and Pitfalls

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Array to List in Java: From Beginner to Pro in 10 Minutes

\n\n

Have you ever written code that worked perfectly with an array, but then suddenly needed a list? Or, even worse, received a mysterious exception when trying to add an element to a list created from an array? If so, you've come to the right place.

\n\n

Converting array to list in Java is one of those tasks that seems simple but hides many surprises. On Stack Overflow, this question has gathered hundreds of thousands of views, and for good reason: behind the apparent simplicity lie important nuances of working with collections.

\n\n

In this article, we'll cover all the legitimate ways to convert an array to a list in Java, learn how to avoid common mistakes, and figure out which method to choose in a specific situation. Let's go!

\n\n

Why is Simple Arrays.asList() a Trap?

\n\n

The most popular way to convert an array to a list in Java is the Arrays.asList() method. It looks deceptively simple:

\n\n
String[] fruits = {"apple", "banana", "cherry"};\nList<String> fruitList = Arrays.asList(fruits);
\n\n

That's it? Almost. But there are three important "buts" that not everyone knows about:

\n\n

1. Fixed Size

\n

The list obtained via Arrays.asList() has a fixed size. Try adding an element:

\n\n
fruitList.add("orange"); // UnsupportedOperationException!
\n\n

You'll get an UnsupportedOperationException. Why? Because under the hood, it's not a real ArrayList, but a built-in implementation that simply wraps the array.

\n\n

2. Changes to the Array Affect the List

\n

The list is a "window" into the original array. Change the array, and the list changes too:

\n\n
fruits[0] = "apricot";\nSystem.out.println(fruitList.get(0)); // apricot
\n\n

This can lead to hard-to-find bugs, especially if the array is passed between methods.

\n\n

3. Primitive Types Don't Work

\n

Try to convert an int array to a list:

\n\n
int[] numbers = {1, 2, 3};\nList<int[]> list = Arrays.asList(numbers); // not List<Integer>, but List<int[]>!
\n\n

Instead of a list of numbers, you get a list containing one element — the entire array. Java cannot automatically box primitives in a list.

\n\n

5 Ways to Convert an Array to a List in Java

\n\n

Let's look at all the main methods for converting an array to a list in Java with examples and explanations.

\n\n

Method 1: Classic Arrays.asList()

\n

Suitable when you need a read-only list or are sure you won't change its size.

\n\n
String[] colors = {"red", "green", "blue"};\nList<String> colorList = Arrays.asList(colors);\n// Works: reading, replacing elements\ncolorList.set(0, "yellow");\n// Doesn't work: adding, removing
\n\n

Method 2: Creating a New ArrayList

\n

The safest way to convert an array to an ArrayList in Java is to create a new instance:

\n\n
String[] animals = {"cat", "dog", "bird"};\nList<String> animalList = new ArrayList<>(Arrays.asList(animals));\n// Now you can add and remove\nanimalList.add("fish"); // OK
\n\n

This approach gives you a fully mutable list, independent of the original array.

\n\n

Method 3: Collections.addAll()

\n

An elegant method for converting an array to a Java collection. Especially useful when the list already exists:

\n\n
String[] cities = {"Moscow", "Paris", "London"};\nList<String> cityList = new ArrayList<>();\nCollections.addAll(cityList, cities);\n// Or in one line:\nList<String> cityList2 = new ArrayList<>(cities.length);\nCollections.addAll(cityList2, cities);
\

Blogs

Book Recommendations