List在remove元素后报java.lang.UnsupportedOperationException
来源:互联网 发布:unity3d陷阱 编辑:程序博客网 时间:2024/04/28 13:59
例子:
package com.test;
import java.util.Arrays;
import java.util.List;
public class TestUnsupported {
public static void main(String[] args) {
String[] s = {
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
};
List a = Arrays.asList(s);
System.out.println(
"a.contains(" + s[0] + ") = " +
a.contains(s[0]));
a.add("eleven"); // Unsupported
a.remove(s[0]); // Unsupported
}
}
运行后,抛出异常如下:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:151)
at java.util.AbstractList.add(AbstractList.java:89)
at com.test.TestUnsupported.main(TestUnsupported.java:28)
解决方法是使用Iterator,或者转换为ArrayList
List arrayList = new ArrayList(a);
参考
When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.
If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.
Even then your code won't work because you'll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read).
You are using AbstractList. ArrayList and Arrays$ArrayList are both types of AbstractList. That's why you get UnsupportedOperationException: Arrays$ArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses).
另外:List转换成字符串并加入分隔符,可以StringUtils的join方法
public String listToString(List list, char separator) {
org.apache.commons.lang.StringUtils.join(list.toArray(),separator);
}
- List在remove元素后报java.lang.UnsupportedOperationException
- List在调用add、remove方法后报java.lang.UnsupportedOperationException
- Java中List.remove报错UnsupportedOperationException
- Java中List.remove报错UnsupportedOperationException
- List报 java.lang.UnsupportedOperationException异常
- Arrays.asList(strArray)转换为List,执行remove(),报异常:java.lang.UnsupportedOperationException深究
- Arrays.asList的到的List进行remove操作报java.lang.UnsupportedOperationException
- Java中List对象调用remove方法报错:UnsupportedOperationException
- ArrayList Iterator remove java.lang.UnsupportedOperationException
- java集合报错:java.lang.UnsupportedOperationException
- Java报异常:java.lang.UnsupportedOperationException
- Java数组转换List后使用用add,remove抛UnsupportedOperationException异常
- List操作的java.lang.UnsupportedOperationException
- 项目中执行ArrayList.remove() 抛出java.lang.UnsupportedOperationException
- list 操作add时候 java.lang.UnsupportedOperationException 错误
- String数组转List,java.lang.UnsupportedOperationException异常
- 关于Arrays.asList()报java.lang.UnsupportedOperationException异常分析
- java.lang.UnsupportedOperationException翻译
- ZKM代码混淆
- Apache Thrift使用总结
- 【C语言天天练(十四)】const关键字详解
- MyBatis的动态SQL详解
- 是时候在 android 上 Go 了吧 !
- List在remove元素后报java.lang.UnsupportedOperationException
- keil s3c440的MMA7455L程序问题
- Spark组件、Halo组件和Flex 4命名空间
- intent跳转的动画效果
- python ---初级学习之安装matplotlib模块
- 管理项目开发记录
- ftp下载文件
- OCP 1Z0 052 128
- AttributeUsage属性