Which fix removes the bug?
List<Integer> ids = Arrays.asList(1, 2); ids.add(3); // UnsupportedOperationException
- Build it with
List.of(1, 2) - Call
ids.addAll(List.of(3)) - Write
ids.add(Integer.valueOf(3)) - Copy it:
new ArrayList<>(ids)
Arrays.asList returns a fixed-size view of an array; add and remove throw. Copying into an ArrayList gives a list that can grow. List.of is fully immutable.
Play Problem Solving ▸