Array to List in Java: From Beginner to Pro in 10 Minutes
\n\nHave 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\nConverting 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\nIn 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\nWhy is Simple Arrays.asList() a Trap?
\n\nThe most popular way to convert an array to a list in Java is the Arrays.asList() method. It looks deceptively simple:
String[] fruits = {"apple", "banana", "cherry"};\nList<String> fruitList = Arrays.asList(fruits);\n\nThat's it? Almost. But there are three important "buts" that not everyone knows about:
\n\n1. Fixed Size
\nThe list obtained via Arrays.asList() has a fixed size. Try adding an element:
fruitList.add("orange"); // UnsupportedOperationException!\n\nYou'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.
2. Changes to the Array Affect the List
\nThe list is a "window" into the original array. Change the array, and the list changes too:
\n\nfruits[0] = "apricot";\nSystem.out.println(fruitList.get(0)); // apricot\n\nThis can lead to hard-to-find bugs, especially if the array is passed between methods.
\n\n3. Primitive Types Don't Work
\nTry to convert an int array to a list:
\n\nint[] numbers = {1, 2, 3};\nList<int[]> list = Arrays.asList(numbers); // not List<Integer>, but List<int[]>!\n\nInstead of a list of numbers, you get a list containing one element — the entire array. Java cannot automatically box primitives in a list.
\n\n5 Ways to Convert an Array to a List in Java
\n\nLet's look at all the main methods for converting an array to a list in Java with examples and explanations.
\n\nMethod 1: Classic Arrays.asList()
\nSuitable when you need a read-only list or are sure you won't change its size.
\n\nString[] 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\nMethod 2: Creating a New ArrayList
\nThe safest way to convert an array to an ArrayList in Java is to create a new instance:
\n\nString[] animals = {"cat", "dog", "bird"};\nList<String> animalList = new ArrayList<>(Arrays.asList(animals));\n// Now you can add and remove\nanimalList.add("fish"); // OK\n\nThis approach gives you a fully mutable list, independent of the original array.
\n\nMethod 3: Collections.addAll()
\nAn elegant method for converting an array to a Java collection. Especially useful when the list already exists:
\n\nString[] 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);\