背景:经常会有这样的需求,我们需要将集合A的值拷贝到集合B,或者是将多个数组转集合再统计值,那么这样是不是可行的呢,刚好昨天有个朋友碰到个错误,我们一起讨论讨论。

正常情况下,我们实现添加集合A的值到集合B中,是这样的:

 

List<String> list3 = new ArrayList<>();
list3.add("1");
List<String> list4 = new ArrayList<>();
list4.add("2");
list3.addAll(list4);

 

这样没毛病,老铁,然而那位朋友说他调用addAll方法,报出了如下错误:

 

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.AbstractList.add(AbstractList.java:148)
 at java.util.AbstractList.add(AbstractList.java:108)
 at java.util.AbstractCollection.addAll(AbstractCollection.java:344)

 

然后他说调试的时候两个集合的值都是正常的,这就奇怪了,于是追踪源头发现他的集合是由数组转换的,举例如下:

 

List<String> list1 = Arrays.asList("1", "2", "3");
List<String> list2 = Arrays.asList("4", "5", "6");
list1.addAll(list2);

 

通常很多人为了操作方便喜欢把数组转换成集合再操作,jdk也刚好有这样的方法,Arrays.asList ,我们看看他的内部实现

/**
    * Returns a fixed-size list backed by the specified array.  (Changes to
    * the returned list "write through" to the array.)  This method acts
    * as bridge between array-based and collection-based APIs, in
    * combination with {@link Collection#toArray}.  The returned list is
    * serializable and implements {@link RandomAccess}.
    *
    * <p>This method also provides a convenient way to create a fixed-size
    * list initialized to contain several elements:
    * <pre>
    *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
    * </pre>
    *
    * @param <T> the class of the objects in the array
    * @param a the array by which the list will be backed
    * @return a list view of the specified array
    */

   @SafeVarargs
   @SuppressWarnings("varargs")
   public static <T> List<T> asList(T... a) {
       return new ArrayList<>(a);
   }

我们通过方法上面的描述可以得知,通过这样调用将数组转换成集合,返回的集合大小是固定的,因此往里面再添加元素肯定报错了。那么解决这个问题的正确姿势是什么,稍微修改下即可:

//错误姿势
List<String> list1 = Arrays.asList("1", "2", "3");
       
List<String> list2 = Arrays.asList("4", "5", "6");
       
list1.addAll(list2);


//正确姿势        
List<String> list3 = new ArrayList<>(Arrays.asList("1", "2", "3"));
       
List<String> list4 = new ArrayList<>(Arrays.asList("1", "2", "3"));
       
list3.addAll(list4);

 

顺便回顾下数组与集合的一些区别:

  • 数组声明了它容纳的元素的类型,而集合不声明。这是由于集合以object形式来存储它们的元素。

  • 一个数组实例具有固定的大小,不能伸缩。集合则可根据需要动态改变大小。

https://mp.weixin.qq.com/s/TfBEh7RERGLTy1w7f5IFeA