$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #252

Which fix removes the bug?

List<Integer> ids = Arrays.asList(1, 2);
ids.add(3); // UnsupportedOperationException
  1. Build it with List.of(1, 2)
  2. Call ids.addAll(List.of(3))
  3. Write ids.add(Integer.valueOf(3))
  4. 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 ▸