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
分析过程
-
通过异常详细我们知道是 不支持操作的异常 出现在 12 行
remove(1)爆出的- 这个 remover 方法应该有所问题
- 需要去类中找实体方法 看看
-
进入 Arrays.asList
好了我们知道,抽象类中的方法体,都在报错,那我们在回到实体类中看看方法有没被重写
到这里问题就很清楚了!