Converting an array to a List in Java: how to do it correctly and without errors?
Hello everyone! I recently started learning Java and encountered a task: I need to convert a regular array (e.g., String[]) to a List. It seems simple, but I ran into several pitfalls.\n\nLet's say I have an array of strings:\n
String[] array = {"a", "b", "c"};\n\nI tried using the Arrays.asList() method:\nList list = Arrays.asList(array); \n\nIt seems to work, but when I tried to add a new element to the list:\nlist.add("d");\nI got an UnsupportedOperationException. Why is that? After all, a List should support adding elements.\n\nI also heard there is a way using new ArrayList<>(Arrays.asList(...)). How is it different from just calling Arrays.a