List直接使用removeAll报错

List直接使用removeAll报错

需要先将list转换才能使用

原因是:

removeAll 方法在 Java 中用于从当前列表中删除另一个列表中存在的所有元素。如果直接对 List 接口的一个实现使用 removeAll 方法抛出异常,可能的原因有:

  1. 不同的List实现 :如果你尝试在不支持该操作的 List 实现上使用 removeAll,例如 Arrays.asList() 创建的列表,则会抛出 UnsupportedOperationException。

  2. 并发修改异常 :如果在迭代过程中尝试修改列表,会抛出 ConcurrentModificationException。

    解决方法:

  • 确保你使用的 List 实现支持 removeAll 操作,如 ArrayList, LinkedList, Vector 等。

    复制代码
      public static void main(String[] args) {
    
          List<String> list1 = Arrays.asList("CS562798662", "CS563279854", "CS563276666");
          List<String> list2 = Arrays.asList("CS562798662", "CS563279854");
          ArrayList<Object> list11 = new ArrayList<>();
          ArrayList<Object> list22 = new ArrayList<>();
          list11.addAll(list1);
          list22.addAll(list2);
    
          list11.removeAll(list22);
          System.out.println(list11);
          
    
      }
相关推荐
happylifetree1 小时前
Python09:核心语法-数据存储与运算-字面量
python
心之语歌2 小时前
Tkinter 画布基本梳理
运维·服务器·python
Wx-bishekaifayuan2 小时前
django个性化旅游路线推荐平台49005-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
万年咸鱼3 小时前
Qt Windows 使用管理员权限运行 Cmd
windows
小白快快跑哦3 小时前
python-字符串全解(六):正则表达式-量词
python·正则表达式·字符串
禹凕3 小时前
机器学习之Selenium(Machina Learning about Selenium)
爬虫·python·selenium·测试工具·机器学习
ebiobiz5 小时前
基于 GD32 Embedded Builder (GEB) 与 Nimmake 的 MCU 工程搭建指南
c++·python·单片机·嵌入式硬件·mcu
老歌老听老掉牙6 小时前
穿越时空的物流密码:VRP与VRPTW的深度剖析及实战求解
python·物流·vrp
FYKJ_20106 小时前
django电影推荐系统55066-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
ShyanZh6 小时前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式