Arrays.asList返回结果执行clear方法抛java.lang.UnsupportedOperationException
来源:互联网 发布:qq号码采集软件 编辑:程序博客网 时间:2024/06/06 03:55
代码
public class ErrorTest { public static void main(String[] args) { String[] strArr=new String[]{"1","2","3"}; List<String> list=Arrays.asList(strArr); list.clear(); System.out.println(list); }}
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractList.removeRange(AbstractList.java:571)
at java.util.AbstractList.clear(AbstractList.java:234)
at cn.htd.goodscenter.test.ErrorTest.main(ErrorTest.java:10)
原因:
Arrays.asList(strArr);返回的List是Arrays类定义的内部类ArrayList,这个ArrayList继承了AbstractList。
@SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }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) { } ... }
AbstractList的clear方法代码如下
public void clear() { removeRange(0, size()); } protected void removeRange(int fromIndex, int toIndex) { ListIterator<E> it = listIterator(fromIndex); for (int i=0, n=toIndex-fromIndex; i<n; i++) { it.next(); it.remove(); } } public ListIterator<E> listIterator(final int index) { rangeCheckForAdd(index); return new ListItr(index); }
ListIterator实际类是ListItr,ListItr类是AbstractList类的一个内部类,代码如下:
private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { cursor = index; } public boolean hasPrevious() { return cursor != 0; } public E previous() { checkForComodification(); try { int i = cursor - 1; E previous = get(i); lastRet = cursor = i; return previous; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } }
ListItr继承了ListItr,我们可以看下ListItr的remove方法
public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } }
重点看 AbstractList.this.remove(lastRet);这一行,调用了AbstractList的remove方法
public E remove(int index) { throw new UnsupportedOperationException(); }
该方法在AbstractList类中并没有实现。
0 0
- Arrays.asList返回结果执行clear方法抛java.lang.UnsupportedOperationException
- arrays.aslist() 方法 java.lang.UnsupportedOperationException
- Arrays.asList() 出现java.lang.UnsupportedOperationException
- Arrays.asList()引起的java.lang.UnsupportedOperationException
- Arrays.asList java.lang.UnsupportedOperationException 异常
- Arrays.asList(strArray)转换为List,执行remove(),报异常:java.lang.UnsupportedOperationException深究
- 关于Arrays.asList()报java.lang.UnsupportedOperationException异常分析
- Arrays.asList()使用---导致java.lang.UnsupportedOperationException分析
- 使用Arrays.asList时出现异常 java.lang.UnsupportedOperationException
- Arrays.asList的到的List进行remove操作报java.lang.UnsupportedOperationException
- Arrays.asList() 得到 集合 执行 add() 和 remove() 抛 UnsupportedOperationException 异常
- Java中的Arrays.asList()方法
- Java Arrays.asList()方法详解
- Arrays.asList不能remove、add等,抛UnsupportedOperationException
- Arrays.asList方法返回的List不能执行add、remove操作
- Java中Arrays的asList()方法
- Java初学习 - Arrays.asList方法总结
- java中的Arrays的asList方法
- Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用
- iOS根据字符串计算UITextView高度
- adb logcat 命令行用法
- Dataguard中日志传输服务
- Linux查找含有某字符串的所有文件
- Arrays.asList返回结果执行clear方法抛java.lang.UnsupportedOperationException
- 派生类继承问题和访问权限问题
- Monkey常用命令集锦
- webRTC学习笔记之有用的链接
- C基础(1)/hello world
- rotate.js
- LeetCode-112. Path Sum
- udhcp源码剖析(四)——DHCP服务器的superloop
- 求斐波那契数列的非递归解法;