Arrays.asList不能remove、add等,抛UnsupportedOperationException
来源:互联网 发布:sqoop导入数据到oracle 编辑:程序博客网 时间:2024/05/16 14:41
我们知道,Arrays.asList返回的是个ArrayList,但是为什么不能修改呢?而且,当我们这么操作的时候会抛UnsupportedOperationException异常
其实,原因有点隐蔽,当我们仔细看Arrays的源码会发现,Arrays自己实现了一个ArrayList,仅仅是名字和常用的那个相同而已,而它返回的数组对象可用的方法就在下边:
@SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] array) { if (array==null) throw new NullPointerException(); a = array; } public int size() { return a.length; } public Object[] toArray() { return a.clone(); } public <T> T[] toArray(T[] a) { int size = size(); if (a.length < size) return Arrays.copyOf(this.a, size, (Class<? extends T[]>) a.getClass()); System.arraycopy(this.a, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } public E get(int index) { return a[index]; } public E set(int index, E element) { E oldValue = a[index]; a[index] = element; return oldValue; } public int indexOf(Object o) { if (o==null) { for (int i=0; i<a.length; i++) if (a[i]==null) return i; } else { for (int i=0; i<a.length; i++) if (o.equals(a[i])) return i; } return -1; } public boolean contains(Object o) { return indexOf(o) != -1; } }而,题目所说的remove、add方法此实现类就没有实现,再看它的父类(AbstractList<E>)中关于这两个方法的是实现:
/** * {@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为什么不能移除、新增项了。
....................................................................
那么,我们该怎么办呢?
方法是有的,我们这么干:
String[] array = new String[]{"1","2","3","4"};List<String> list = new ArrayList<String>(Arrays.asList(array));这样就可以对 list 进行add、remove操作了。
0 0
- Arrays.asList不能remove、add等,抛UnsupportedOperationException
- 关于 Arrays.asList add、remove 操作的 UnsupportedOperationException
- Arrays.asList() 得到 集合 执行 add() 和 remove() 抛 UnsupportedOperationException 异常
- Arrays.asList() 返回的list不能add,remove
- Arrays.asList() 返回的list不能add,remove
- 为什么Arrays.asList(T...t)不能add和remove?
- Arrays.asList()为什么不能支持add()和remove()
- Arrays.asList()返回的List不能add和remove
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- Arrays.asList方法返回的List不能执行add、remove操作
- Arrays.asList() 返回的list不能add,remove以及慎用java.util.Collections.copy()方法
- Arrays.aslist新建的list集合不能add(),set(),remove()你知道吗?
- # Arrays.asList 返回的list 不能使用 add,set,remove 方法
- 解决Arrays.asList不能用add或remove
- Arrays.asList().add()异常
- Arrays.asList(strArray)转换为List,执行remove(),报异常:java.lang.UnsupportedOperationException深究
- 使用Jacob实现word转PDF
- 汇编语言(五) 汇编代码在内存中的结构( 8086 + masm 编译器)
- 【数据表间关联关系】 一对多、多对一、一对一、多对多
- GUI(图形用户界面)
- JAVA内存结构之运行时栈帧结构
- Arrays.asList不能remove、add等,抛UnsupportedOperationException
- android之蓝牙控制小四轴飞行器
- Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(一)
- leetcode #79 in cpp
- jsp页面中的post提交方式
- Solr中配置文件schema.xml之copyField 与multiValued用途
- PlantUML 使用
- 压缩图片尺寸
- 大型网站架构演化发展历程