gmh*_*mhk 58 java collections arraylist java-ee
在Java中,如何ArrayList在初始化之后创建只读(以便没有人可以添加元素,编辑或删除元素)?
Mar*_*ope 112
通行证ArrayList进入Collections.unmodifiableList().它返回指定列表的不可修改视图.只使用此返回List,而不是原始ArrayList.
将列表对象传递给Collections.unmodifiableList(). 请参阅下面的示例。
import java.util.*;
public class CollDemo
{
public static void main(String[] argv) throws Exception
{
List stuff = Arrays.asList(new String[] { "a", "b" });
List list = new ArrayList(stuff);
list = Collections.unmodifiableList(list);
Set set = new HashSet(stuff);
set = Collections.unmodifiableSet(set);
Map map = new HashMap();
map = Collections.unmodifiableMap(map);
System.out.println("Collection is read-only now.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39226 次 |
| 最近记录: |