List<Integer> list = Collections.EMPTY_LIST;
用Collections.EMPTY_LIST声明的list不能进行add操作否则会报错: java.lang.UnsupportedOperationException
Collections.EMPTY_LIST 的实现
public static final List EMPTY_LIST = new EmptyList<>();
EmptyList是Collections的一个静态内部类,其并没有实现add、remove方法,因此使用Collections.EMPTY_LIST声明的list不能进行add、remove等操作。
本文详细解析了Java中Collections.EMPTY_LIST的实现原理,该列表为只读,尝试进行add或remove操作会导致UnsupportedOperationException。EmptyList是Collections的静态内部类,未实现修改方法,确保了列表的不可变性。了解这一特性对于避免运行时错误至关重要。
1434

被折叠的 条评论
为什么被折叠?



