Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line contains the error?

import java.util.*;

public class Sample57
{
    public static void main(String[] args)
    {
        List<String> ro = Collections.unmodifiableList(new ArrayList<String>());
        assert ro.isEmpty();
        ro.add("x");
        System.out.println(ro.size());
    }
}

  • A.ro.add("x");
  • B.assert ro.isEmpty();
  • C.List ro = Collections.unmodifiableList(new ArrayList());
  • D.Nothing here is wrong.

Answer: A

An unmodifiable view rejects every mutation with UnsupportedOperationException, so the add is the error. The assertion above it is correct and passes. Note the wrapper is a view, not a copy: changes made through the original list would still be visible here.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.