使 ArrayList 只读 [英] make ArrayList Read only
本文介绍了使 ArrayList 只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Java 中,如何使 ArrayList
在初始化后变为只读(这样就没有人可以添加元素、编辑或删除元素)?
In Java, how can you make an ArrayList
read-only (so that no one can add elements, edit, or delete elements) after initialization?
推荐答案
将 ArrayList
传入 Collections.unmodifiableList()
.它返回指定列表的不可修改视图.只使用这个返回的List
,不要使用原来的ArrayList
.
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
.
这篇关于使 ArrayList 只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文