Converting an Array to ArrayList: Why It's a Perpetual Headache for Java Developers
\n\nAnyone 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.
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.
Let's go! (or as Java developers say — let's dive into the code)
\n\nMethod 1: The Classic — Arrays.asList()
\n\nThe most popular and shortest way. You've probably seen it:
\n\nString[] fruits = {"apple", "banana", "cherry"};\nList<String> fruitList = Arrays.asList(fruits);\n\nLooks 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.
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
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
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.
How to get a full-fledged ArrayList from Arrays.asList()?
\n\nSimply wrap the result in a constructor:
\n\nString[] fruits = {"apple", "banana", "cherry"};\nArrayList<String> fruitList = new ArrayList<>(Arrays.asList(fruits));\nfruitList.add("date"); // Now works without errors\n\nThis technique is the sweet spot. You use the concise Arrays.asList syntax but get a fully dynamic collection.
Method 2: Collections.addAll() — Speed and Control
\n\nIf 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:
String[] fruits = {"apple", "banana", "cherry"};\nArrayList<String> fruitList = new ArrayList<>(fruits.length);\nCollections.addAll(fruitList, fruits);\n\nWhat'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.
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
Cons:
\n- \n
- The code is a bi