快速建手机网站,了解目前网站建设情况,阿克苏网站怎么做seo,动态图表网站1. 问题说明
记录一下遇到的这个bug#xff0c;下面是段个简化后的问题重现代码。
public class Test {public static void main(String[] args) {ListInteger list  Arrays.asList(1, 2, 3);list.add(4);}
}2. 原因分析
我们看一下Arrays.asList(…) 的源码#xff…1. 问题说明
记录一下遇到的这个bug下面是段个简化后的问题重现代码。
public class Test {public static void main(String[] args) {ListInteger list  Arrays.asList(1, 2, 3);list.add(4);}
}2. 原因分析
我们看一下Arrays.asList(…) 的源码
/*
- Returns a fixed-size list backed by the specified array. Changes made to the array will be visible in the returned list, and changes made to the list will be visible in the array. The returned list is Serializable and implements RandomAccess.
- The returned list implements the optional Collection methods, except those that would change the size of the returned list. Those methods leave the list unchanged and throw UnsupportedOperationException.
- If the specified arrays actual component type differs from the type parameter T, this can result in operations on the returned list throwing an ArrayStoreException.
*/
SafeVarargs
/varargs/
public static T ListT asList(T... a) {return new ArrayList(a);
}//  上面的 return new ArrayList(a)中的ArrayList源码
private static class ArrayListE extends AbstractListE implements RandomAccess, java.io.Serializable { // ... 
}从方法上给出的注释信息中的第2条可以知道返回的其实是 java.util.Arrays$ArrayList也就是一个内部类并不是 java.util.ArrayList。它内部基于数组实现只能固定大小不支持 add()、addAll()、remove() 等结构性操作 。如果如果你需要一个可变、可以添加或删除元素的列表请用
ListInteger list  new ArrayList(Arrays.asList(1, 2, 3));
list.add(3); // 安全可行3. 补充说明
在高并发或多线程环境下如果基础数组发生变化Arrays.asList 返回的列表内容也会变化可能引发数据不一致。List.of(...)Java 9是完全不可变的列表任何变结构操作都会立即抛异常而 Arrays.asList 是“可修改元素但不可改大小”。