Java › Arrays & Multidimensional Arrays

How do you convert between an array and a List (ArrayList) in both directions, and what pitfalls exist with Arrays.asList()?

Intermediate

Answer

Arrays.asList(array) returns a fixed-size List backed directly by the original array (not a true resizable ArrayList -- calling add()/remove() throws UnsupportedOperationException, and mutating the array is reflected in the list view and vice versa); to get a genuinely independent, resizable list, wrap it in new ArrayList<>(Arrays.asList(array)); converting back from a List to an array uses list.toArray(new String[0]) (or the newer list.toArray(String[]::new)).
Integer[] arr = {1, 2, 3};
List<Integer> fixedView = Arrays.asList(arr);  // backed by arr, fixed-size
// fixedView.add(4);  // throws UnsupportedOperationException!

List<Integer> resizable = new ArrayList<>(Arrays.asList(arr));  // truly independent, resizable copy
resizable.add(4);  // works fine

Integer[] backToArray = resizable.toArray(new Integer[0]);
Real-world example A developer's attempt to call .add() on a list obtained directly from Arrays.asList() throws an unexpected UnsupportedOperationException in production, a common pitfall traced to Arrays.asList() returning a fixed-size view rather than a genuine resizable ArrayList.

Common follow-ups: Why does Arrays.asList() exist as a fixed-size view instead of just returning a full ArrayList directly?;What's the difference in behavior between toArray(new T[0]) and the newer toArray(T[]::new) method reference syntax?