0
点赞
收藏
分享

微信扫一扫

UnsupportedOperationException

zhyuzh3d 2022-02-20 阅读 66
    public static void main(String[] args)  {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        list.remove(1);
    }
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.remove(AbstractList.java:161)
	at com.lfweixiao.Test01.main(Test01.java:12)

原因,处理办法

原因: 这里的Arrays.asList返回的是 Arrays 中的 内部类 ArrayList此类继承了AbstractList却没重写 remove(int index)、set(int index, E element) 、add(int index, E element)

**解决方式:**直接 new 来创建 ArrayList

分析过程

  1. 通过异常详细我们知道是 不支持操作的异常 出现在 12 行
    remove(1)爆出的

    1. 这个 remover 方法应该有所问题
    2. 需要去类中找实体方法 看看
  2. 进入 Arrays.asList

好了我们知道,抽象类中的方法体,都在报错,那我们在回到实体类中看看方法有没被重写

在这里插入图片描述

到这里问题就很清楚了!

举报

相关推荐

0 条评论