Collections.unmodifiableList and defensive copy

后端 未结 8 1954
一向
一向 2020-12-13 04:30

If I write

List a1 = Arrays.asList(1, 2, 3);
List a2 = Collections.unmodifiableList(a1);

a2 is r

8条回答
  •  [愿得一人]
    2020-12-13 04:45

    The idea is that you can't modify the list via a2.

    Modifying the a1 list will indeed modify what you see in a2 - this is intended.

    Just dont have a public way to access the list a1, and you should have what you want :)

提交回复
热议问题