asList转换为集合

来源:互联网 发布:中国梦之队服装淘宝网 编辑:程序博客网 时间:2024/05/17 13:40


 java中我们经常会使用Arrays.asList(数组);将数组转换为List类型,但是却不能对转换后的List进行增删。示例如下:

  1. import java.util.*;  
  2. class  ArraysDemo  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6. //      定义一个Sting型数组  
  7.         String[] arr={"abc","abcde","desf","dfdf","fdfee"};  
  8. //      将数组转换为List集合  
  9.         List<String> list=Arrays.asList(arr);  
  10. //      获取第一元素的值  
  11.         System.out.println(list.get(1));  
  12. //      增加元素  
  13.         list.add("test");  
  14.     }  
  15. }  


运行结果:  

  1. abcde  
  2. Exception in thread "main" java.lang.UnsupportedOperationException  
  3.         at java.util.AbstractList.add(Unknown Source)  
  4.         at java.util.AbstractList.add(Unknown Source)  
  5.         at ArraysDemo.main(ArraysDemo.java:13)<pre name="code" class="java">

运行结果报出了UnsuportedOperationException,查阅如下Arrays.asList()方法源码可以得知,
Arrays内部实现了私有内部静态类,而且没有实现增删的方法。所以通过asList()生成的List没有增删方法。
  1.  @SafeVarargs  
  2.     public static <T> List<T> asList(T... a) {  
  3.         return new ArrayList<>(a);  
  4.     }  

  5.     /**  
  6.      * @serial include  
  7.      */  
  8. //定义私有内部静态类,继承了AbstractList虚类  
  9.     private static class ArrayList<E> extends AbstractList<E>  
  10.         implements RandomAccess, java.io.Serializable  
  11.     {  
  12.         private static final long serialVersionUID = -2764017481108945198L;  
  13.         private final E[] a;  
  14.   
  15.         ArrayList(E[] array) {  
  16.             if (array==null)  
  17.                 throw new NullPointerException();  
  18.             a = array;  
  19.         }  
  20.   
  21.         public int size() {  
  22.             return a.length;  
  23.         }  
  24.   
  25.         public Object[] toArray() {  
  26.             return a.clone();  
  27.         }  
  28.   
  29.         public <T> T[] toArray(T[] a) {  
  30.             int size = size();  
  31.             if (a.length < size)  
  32.                 return Arrays.copyOf(this.a, size,  
  33.                                      (Class<? extends T[]>) a.getClass());  
  34.             System.arraycopy(this.a, 0, a, 0, size);  
  35.             if (a.length > size)  
  36.                 a[size] = null;  
  37.             return a;  
  38.         }  
  39.   
  40.         public E get(int index) {  
  41.             return a[index];  
  42.         }  
  43.   
  44.         public E set(int index, E element) {  
  45.             E oldValue = a[index];  
  46.             a[index] = element;  
  47.             return oldValue;  
  48.         }  
  49.   
  50.         public int indexOf(Object o) {  
  51.             if (o==null) {  
  52.                 for (int i=0; i<a.length; i++)  
  53.                     if (a[i]==null)  
  54.                         return i;  
  55.             } else {  
  56.                 for (int i=0; i<a.length; i++)  
  57.                     if (o.equals(a[i]))  
  58.                         return i;  
  59.             }  
  60.             return -1;  
  61.         }  
  62.   
  63.         public boolean contains(Object o) {  
  64.             return indexOf(o) != -1;  
  65.         }  
  66.     }  



原创粉丝点击