List加入和删除元素。(在list中add时出现UnsuppotedOperationException)
2018-03-18 09:21
465 查看
第一种删除方式:迭代器
public class Test { public static void main(String args[]){ List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("b"); Iterator<String> iter = list.iterator(); while(iter.hasNext()){ String s = iter.next(); if(s.equals("b")){ iter.remove(); } } System.out.println(list); } }
运行结果为【a,c】
第二种删除方式:记录后统一删除
public class Test { public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("b"); List<String> list_remove = new ArrayList<String>(); for(String s : list){ if("b".equals(s)){ list_remove.add(s); //把要移除的统一记录在一个集合 } } //统一移除 list.removeAll(list_remove); System.out.println(list); } }
输出结果:【a,c】
错误删除的例子1,2
for (String s : list) { //移除元素后,无法预知到元素的位置。 if (s.equals("b")) { list.remove(s); } } for(int i=0;i<list.size();i++){ if(list.get(i).equals("b")){ list.remove(i); } } //输出[a, b, c, c]。size()的问题
向List中添加元素时出现UnsuppotedOperationException
List<String> words = new ArrayList<>(); String[] word = input.split("[\\t \\n]+"); List<String> list = Arrays.asList(word); for(int i = 0; i < words.size()-1;i++) { String nowStr = words.get(i); String nextStr = words.get(i+1); String bridge = getTwoEdgeLong(nowStr, nextStr); if(bridge != "") { words.add(i+1, bridge);//报错 i++; } }
原因是:不是每一个LIst的实现都能够支持add方法。最常见的例子就是Arrays.asList()返回的List列表。不支持在结构上的更改。在文档中记载:
Returns a fixed-size list backed by the specified array.
因此解决方法是:(将asList方法改为循环逐个添加元素)
List<String> words = new ArrayList<>(); String[] word = input.split("[\\t \\n]+"); for(int i = 0; i < word.length ;i++) {//更改为循环添加的方法。 words.add(word[i]); } for(int i = 0; i < words.size()-1;i++) { String nowStr = words.get(i); String nextStr = words.get(i+1); String bridge = getTwoEdgeLong(nowStr, nextStr); if(bridge != "") { words.add(i+1, bridge);/*add method may not supported by special implement*/ i++; } }
相关文章推荐
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- Unity在Dictionary中删除修改元素时出现InvalidOperationException: out of sync
- 数组转换为List后add或remove出现UnsupportedOperationException异常分析
- 数组转换为List后add或remove出现UnsupportedOperationException异常分析
- Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常
- 数组转换为List(Arrays.asList)后add或remove出现UnsupportedOperationException
- 使用倒序循环删除list中重复的相同元素 、 string的split使用、返回list(string)中出现多次的某元素的indexs...
- List在调用add、remove方法后报java.lang.UnsupportedOperationException
- 关于STL的list,vector等用迭代器iterator,用erase删除元素出现的问题。
- Arrays.asList() 出现java.lang.UnsupportedOperationException
- Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
- list,set等集合遍历时,不能remove集合中的元素。需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAll(list/set)操作
- 使用Arrays.asList时出现异常 java.lang.UnsupportedOperationException
- Arrays.asList不能remove、add等,抛UnsupportedOperationException
- List在remove元素后报java.lang.UnsupportedOperationException
- java.util.ConcurrentModificationException---用迭代器遍历Map时删除其中元素出现异
- Arrays.asList() 得到 集合 执行 add() 和 remove() 抛 UnsupportedOperationException 异常
- 循环List删除元素抛ConcurrentModificationException异常原因分析