Arrays.asList()返回只读的List
2016-01-15 15:05
330 查看
List<Integer> list = Arrays.asList(1, 2, 3);
list.clear(); // throws java.lang.UnsupportedOperationException
对Arrays.asList()转化的List是一个固定长度的List,不支持add() remove() clear()等操作
[b]Arrays.asList()返回的是一个ArrayList对象[/b]
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
而这个类继承自AbstractList类
在AbstractList中而这个类中实现的所有对list的数据编辑操作都会抛出UnsupportedOperationException异常
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
throw new UnsupportedOperationException();
}
所以Arrays.asList()返回的list是只读的并不能对list的数据进行相关修改!
list.clear(); // throws java.lang.UnsupportedOperationException
对Arrays.asList()转化的List是一个固定长度的List,不支持add() remove() clear()等操作
[b]Arrays.asList()返回的是一个ArrayList对象[/b]
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
而这个类继承自AbstractList类
在AbstractList中而这个类中实现的所有对list的数据编辑操作都会抛出UnsupportedOperationException异常
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
throw new UnsupportedOperationException();
}
所以Arrays.asList()返回的list是只读的并不能对list的数据进行相关修改!
相关文章推荐
- AngularJs表单验证
- python中if __name__ == '__main__': 的解析
- NSClassFromString和NSStringFromClass
- JAVA读写文件
- JAVA操作Excel
- leveldb代码阅读(11)——levelDB生成的文件
- 请求
- ListView单选和多选模式完全解析
- js 类型检测
- php上传图片
- 华为OJ:在字符串中找出连续最长的数字串
- 汇编入门学习笔记(二)
- 本人的开发中的一些笔记(一)js判断页面中多个checkbox是否至少被选中一个
- android.graphics.Camera
- wpf datagrid中列或者行头的下划线显示的问题
- C++ Library Installation
- JNI初入手
- PHP 7 探针的安装与测试
- ZeroMQ安装
- Retrofit的分析以及实现步骤