Java使用Arrays.asList报UnsupportedOperationException的解决

时间:2022-09-03 11:49:48

项目场景:

查询多个名销售的销售业绩,上层要求要在查询销售的业绩同事也要查看到每年的年度销售冠军,于是前端传递的是以“,”分割开的字符串。测试的时候就报错java.lang.UnsupportedOperationException的异常

代码展示:

?
1
2
3
4
5
6
7
8
   // 这里隐藏了查询条件,所以就写死了
List<String>  performId=new ArrayList<>();
performId.add("701728881476112384");
performId.add("701728881497083904");
String[] agentIds = StringUtils.split(agentId, ",");
List<String> agentIdList = Arrays.asList(agentIds);
// 后面同事没有注意,就直接展示查询出来就加入
agentIdList.addAll(performId);

原因分析:

于是我们查看了源码,通过**Arrays.asList(T …a)**创建的 **return new ArrayList<>(a);**以为是java.util包下的,所以就对它增删改了。查看源码发现,该方法并不支持增删改 源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable{
       private static final long serialVersionUID = -2764017481108945198L;
       private final E[] a;
       ArrayList(E[] array) {
           a = Objects.requireNonNull(array);
       }
       @Override
       public int size() {
           return a.length;
       }
       @Override
       public Object[] toArray() {
           return a.clone();
       }
       @Override
       @SuppressWarnings("unchecked")
       public <T> T[] toArray(T[] a) {
           int size = size();
           if (a.length < size)return Arrays.copyOf(this.a, size,(Class<? extends T[]>) a.getClass());
           System.arraycopy(this.a, 0, a, 0, size);
           if (a.length > size) a[size] = null;
           return a;
       }
       @Override
       public E get(int index) {
           return a[index];
       }
       @Override
       public E set(int index, E element) {
           E oldValue = a[index];
           a[index] = element;
           return oldValue;
       }
 
       @Override
       public int indexOf(Object o) {
           E[] a = this.a;
           if (o == null) {
               for (int i = 0; i < a.length; i++)
                   if (a[i] == null) return i;
           } else {
             for (int i = 0; i < a.length; i++)
                  if (o.equals(a[i])) return i;
           }
           return -1;
       
       @Override
       public boolean contains(Object o) {
           return indexOf(o) != -1;
       }
       @Override
       public Spliterator<E> spliterator() {
           return Spliterators.spliterator(a, Spliterator.ORDERED);
       }
       @Override
       public void forEach(Consumer<? super E> action) {
           Objects.requireNonNull(action);
           for (E e : a) {
               action.accept(e);
           }
       }
       @Override
       public void replaceAll(UnaryOperator<E> operator) {
           Objects.requireNonNull(operator);
           E[] a = this.a;
           for (int i = 0; i < a.length; i++) {
               a[i] = operator.apply(a[i]);
           }
       }
 
       @Override
       public void sort(Comparator<? super E> c) {
           Arrays.sort(a, c);
       }
   }

通过以上源码发现,Arrays内部实现的ArrayList并未实现增删改等的操作,继承了 AbstractList.class 类中抛出的 UnsupportedOperationException异常。源码如下:

?
1
2
3
4
##只粘贴了部分源码,详情可以去AbstractList.class中查看
public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

总结:

Arrays.asList(T .....a)不能进行增删改等操作。在使用一下类的时候,看下源码会避免一些代码层的坑。

到此这篇关于Java使用Arrays.asList报UnsupportedOperationException的解决的文章就介绍到这了,更多相关Arrays.asList报UnsupportedOperationException内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_36618541/article/details/115794331