make ArrayList Read only

后端 未结 4 1675
温柔的废话
温柔的废话 2020-12-05 09:30

In Java, how can you make an ArrayList read-only (so that no one can add elements, edit, or delete elements) after initialization?

4条回答
  •  难免孤独
    2020-12-05 09:46

    Pass the ArrayList into Collections.unmodifiableList(). It returns an unmodifiable view of the specified list. Only use this returned List, and never the original ArrayList.

提交回复
热议问题