How to Convert an Array to ArrayList in Java: 4 Ways with Examples

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

Converting an Array to ArrayList: Why It's a Perpetual Headache for Java Developers

\n\n

Anyone who has ever written Java has encountered this situation: you have a ready-made array String[] or int[], but the task requires a flexible ArrayList with its add(), remove() methods and dynamic size. It seems like a small thing, but this very question on Stack Overflow has gathered tens of thousands of views and a ton of votes. Why? Because, as often happens in Java, there is an "obvious" solution that hides pitfalls.

\n\n

In this article, we won't just give you ready-made code. We'll go through all the ways to create an ArrayList from an array in Java, analyze their pros and cons, performance, and most importantly — when to use which option. After reading, you'll be able to confidently choose the right tool for your task and avoid running into UnsupportedOperationException at the most unexpected moment.

\n\n

Let's go! (or as Java developers say — let's dive into the code)

\n\n

Method 1: The Classic — Arrays.asList()

\n\n

The most popular and shortest way. You've probably seen it:

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

Looks nice, right? But there's a catch. The Arrays.asList() method returns not java.util.ArrayList, but its own internal static version — java.util.Arrays.ArrayList. This is a fixed-size list that does not support resizing operations. If you try to do fruitList.add("date"), you'll get UnsupportedOperationException.

\n\n

Pros:

\n
    \n
  • Extremely short and readable code.
  • \n
  • Works as a bridge between an array and the Collections API.
  • \n
  • Changes in the list are reflected in the original array (and vice versa).
  • \n
\n\n

Cons:

\n
    \n
  • Fixed size — you cannot add or remove elements.
  • \n
  • The returned list is a "wrapper" over the array, not a full-fledged ArrayList.
  • \n
\n\n

When to use: When you need a read-only list, or when you know for sure that the size won't change. For example, for passing to a method that accepts List<?> and does not modify it.

\n\n

How to get a full-fledged ArrayList from Arrays.asList()?

\n\n

Simply wrap the result in a constructor:

\n\n
String[] fruits = {"apple", "banana", "cherry"};\nArrayList<String> fruitList = new ArrayList<>(Arrays.asList(fruits));\nfruitList.add("date"); // Now works without errors
\n\n

This technique is the sweet spot. You use the concise Arrays.asList syntax but get a fully dynamic collection.

\n\n

Method 2: Collections.addAll() — Speed and Control

\n\n

If you're after performance or simply want to avoid double copying (first into a fixed-size list, then into an ArrayList), there's the Collections.addAll() method:

\n\n
String[] fruits = {"apple", "banana", "cherry"};\nArrayList<String> fruitList = new ArrayList<>(fruits.length);\nCollections.addAll(fruitList, fruits);
\n\n

What's happening here? We immediately create an ArrayList with the required initial capacity (to avoid unnecessary internal array resizing), and then copy all elements in one pass. The addAll method works faster than a for loop because it calls System.arraycopy() at a low level.

\n\n

Pros:

\n
    \n
  • High speed — almost at the level of direct memory copying.
  • \n
  • You immediately get a full-fledged ArrayList with modification capabilities.
  • \n
  • You can pre-set the list's capacity.
  • \n
\n\n

Cons:

\n
    \n
  • The code is a bi

Blogs

Book Recommendations