Avatar ·

How to create an ArrayList from an array in Java? Best ways and pitfalls

Hello everyone! I recently started learning Java and encountered this problem: I have an array of strings, for example: ```java String[] array = {"apple", "banana", "cherry"}; ``` I want to turn it into an ArrayList so I can conveniently add and remove elements. I tried doing this: ```java ArrayList list = new ArrayList<>(array); ``` But the compiler gives an error. Then I found the `Arrays.asList()` method and did this: ```java List list = Arrays.asList(array); ``` This seems to work, but when I try to add a new element via `list.add("orange")`, it throws `UnsupportedOperationException`. Why? After all, ArrayList should support adding elements. I also saw that some people use `Collections.addAll()`, and others use a for loop. Which way is correct? And in general