对Arrays.asList()返回的List进行操作之后报错Caused by: java.lang.UnsupportedOperationException

让我们来看一下Arrays.asList的源码:

/**
* Returns a {@code List} of the objects in the specified array. The size of the
* {@code List} cannot be modified, i.e. adding and removing are unsupported, but
* the elements can be set. Setting an element modifies the underlying
* array.
*
* @param array
* the array.
* @return a {@code List} of the elements of the specified array.
*/
@SafeVarargs
public static <T> List<T> asList(T... array) {
return new ArrayList<T>(array);
} private static class ArrayList<E> extends AbstractList<E> implements
List<E>, Serializable, RandomAccess { private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] storage) {
if (storage == null) {
throw new NullPointerException("storage == null");
}
a = storage;
} @Override
public boolean contains(Object object) {
if (object != null) {
for (E element : a) {
if (object.equals(element)) {
return true;
}
}
} else {
for (E element : a) {
if (element == null) {
return true;
}
}
}
return false;
} @Override
public E get(int location) {
try {
return a[location];
} catch (ArrayIndexOutOfBoundsException e) {
throw java.util.ArrayList.throwIndexOutOfBoundsException(location, a.length);
}
} @Override
public int indexOf(Object object) {
if (object != null) {
for (int i = 0; i < a.length; i++) {
if (object.equals(a[i])) {
return i;
}
}
} else {
for (int i = 0; i < a.length; i++) {
if (a[i] == null) {
return i;
}
}
}
return -1;
} @Override
public int lastIndexOf(Object object) {
if (object != null) {
for (int i = a.length - 1; i >= 0; i--) {
if (object.equals(a[i])) {
return i;
}
}
} else {
for (int i = a.length - 1; i >= 0; i--) {
if (a[i] == null) {
return i;
}
}
}
return -1;
} @Override
public E set(int location, E object) {
E result = a[location];
a[location] = object;
return result;
} @Override
public int size() {
return a.length;
} @Override
public Object[] toArray() {
return a.clone();
} @Override
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
public <T> T[] toArray(T[] contents) {
int size = size();
if (size > contents.length) {
Class<?> ct = contents.getClass().getComponentType();
contents = (T[]) Array.newInstance(ct, size);
}
System.arraycopy(a, 0, contents, 0, size);
if (size < contents.length) {
contents[size] = null;
}
return contents;
}
}

索噶,原来返回的是Arrays类的静态内部类!这样能会报错也就正常咯。

Caused by: java.lang.UnsupportedOperationException的更多相关文章

  1. javax.net.ssl.SSLException: java.lang.UnsupportedOperationException

    Loading KeyStore C:\Tool\jdk1.7.0_71\jre\lib\security\jssecacerts... Opening connection to www.googl ...

  2. Cause: java.lang.UnsupportedOperationException

    运行web项目的时候出现以下错误: ### Cause: java.lang.UnsupportedOperationException    at org.mybatis.spring.MyBati ...

  3. Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常

    String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...

  4. java.lang.UnsupportedOperationException: Unable to create instance of org.fisco.bcos.web3j.abi.datatypes.generated.Int256

    Contract Address : 0x967f92adc229b77dda64b42af21ea1ff1b0702eb Unable to create instance of org.fisco ...

  5. 【异常】Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException

    Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException ...

  6. 编译异常 Caused by: java.lang.UnsupportedClassVersionError:

    Caused by: java.lang.UnsupportedClassVersionError: com/sumingk/platform/service/impl/ServiceSysPerso ...

  7. java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12

    最近使用Android Studio开发一个新项目,刚做完几个界面,跑在android 5.0上面很正常,因为都是挺简单的布局,本以为应该不存在兼容性问题(Flag啊). 偶然用了一个4.x的实机测试 ...

  8. Caused by: java.lang.NoClassDefFoundError:

    tomcat启动不了 报错信息头如下: Caused by: java.lang.NoClassDefFoundError: at java.lang.Class.getDeclaredMethods ...

  9. android 自定义View Caused by: java.lang.ClassNotFoundException: Didn't find class

    在android studio中, 自定义View 时,出现 Caused by: java.lang.ClassNotFoundException: Didn't find class 在查看包名和 ...

随机推荐

  1. Inkpad绘图原理浅析

    本文新版本已转移到开源中国,欢迎前往指导. Inkpad是一款非常优秀的iPad矢量绘图软件,保管你一看见就忘不了.我的感觉是“一览众山小”.“相见甚晚”,以至于我写的TouchVG就是“小巫见大巫” ...

  2. unix/linux中图形界面那些事

    我们知道unix/linux刚开始的时候是没有图形界面的,随着时代的发展,排版.制图.多媒体应用越来越普遍了,这些需求都需要用到图形界面(Graphical User Interface).为此,MI ...

  3. hdu 2152

    题目大意:本题是中文题.读者可以直接到OJ上去阅读.提议并不难理解 代码如下: /* * 2152_1.cpp * * Created on: 2013年8月9日 * Author: Administ ...

  4. codeforces C. Diverse Permutation(构造)

    题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个 |p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 思路: 保证相邻的两个数的差值的 ...

  5. Java 集合系列15之 Set架构

    前面,我们已经系统的对List和Map进行了学习.接下来,我们开始可以学习Set.相信经过Map的了解之后,学习Set会容易很多.毕竟,Set的实现类都是基于Map来实现的(HashSet是通过Has ...

  6. Django--BBS项目

    需求 模仿抽屉()和虎嗅()做一个网站bbs 知识点 Django知识: models表字段第一个属性可以用 2 3 from django.contrib.auth.models import Us ...

  7. 谈HTTP的KeepAlive

    为什么要使用KeepAlive? 终极的原因就是需要加快客户端和服务端的访问请求速度.KeepAlive就是浏览器和服务端之间保持长连接,这个连接是可以复用的.当客户端发送一次请求,收到相应以后,第二 ...

  8. MySQL 备份与还原详解

    相关阅读: MySQL备份和恢复具体实施 http://www.linuxidc.com/Linux/2012-12/76257.htm MySQL备份与恢复的三种方法总结 http://www.li ...

  9. 统一者管理员指南(Unifier Administration Guide)中文

    统一者管理员指南 Unifier Administration Guide 2014年6月 发布 2014年11月翻译 10.0版本 10.0.1译 关于译者 翻译者QQ:77811970 Email ...

  10. 使用BOM 的window对象属性打开新窗口

    ★  示例1 要求:弹出新窗口,并向新窗口写入动态HTML代码 代码 <buttononclick="btnOpen()">打开新窗口</button> & ...