List selection = getSelectedObjects();
List al=new ArrayList();

for(Object o: selection){
if(o instanceof GraphicalBasicBlockEditPart){
GraphicalBasicBlockEditPart gb=(GraphicalBasicBlockEditPart)o;
if(((GraphicalBasicBlock)gb.getModel()).getParent() instanceof GraphicalCompositeBlock){
}else{
al.add(o);
}
}
}
try{
selection.removeAll(al); }catch(Exception e){
System.out.println(e.getStackTrace());
}为什么总是输出异常java.lang.UnsupportedOperationException

解决方案 »

  1.   

    问题简化一下:
    List selection = getSelectedObjects();
    List al=new ArrayList();

    for(Object o: selection){
    if(o满足某种条件){
    al.add(o);
    }
    }
    try{
    selection.removeAll(al);
                    }catch(Exception e){
    System.out.println(e.getStackTrace());
    }为什么总是输出异常java.lang.UnsupportedOperationException
      

  2.   

    List不支持removeAll()方法。
    可用其它方法实现你的功能,如写个循环,用remove方法
    或改用Set接口
      

  3.   

    removeAll方法没问题的,估计是你的selection是不可修改的
    getSelectedObjects();这个方法是不是返回了只读的list
    比如
    List getSelectedObjects() {
        List list = new ArrayList();
        list.add("xxx");
        return Collections.unmodifiableList(list); //返回只读的list
    }
    这样的话,外面是不能修改它的 
      

  4.   

    这个List实际上是ArrayList的一个实例。它的确是支持 removeAll()方法的。问题不在这里。问题在于你使用 for (Object o in collection)的时候,实际上jdk是使用Iterator来遍历这个collection的。使用Iterator的时候,不可以添加或删除Iterator要遍历的集合的元素。你可以操作集合的元素,修改这个元素的某些属性,但是你不能把它从集合中去掉。也不能往集合里面添加新的元素。