一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java 源碼分析Arrays.asList方法詳解

java 源碼分析Arrays.asList方法詳解

2020-06-26 16:08王孟君 JAVA教程

這篇文章主要介紹了java 源碼分析Arrays.asList方法詳解的相關資料,需要的朋友可以參考下

最近,抽空把java Arrays 工具類的asList 方法做了源碼分析,在網上整理了相關資料,記錄下來,希望也能幫助讀者!

Arrays工具類提供了一個方法asList, 使用該方法可以將一個變長參數或者數組轉換成List 。

其源代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * 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 a the array by which the list will be backed
 * @return a list view of the specified array
 */
@SafeVarargs
public static <T> List<T> asList(T... a) {
 return new ArrayList<>(a);
}

問題發現

根據上述方法的描述,我們先來編寫幾個例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
 public static void main(String[] args) {
  
  /**使用變長參數*/
  List<String> array1 = Arrays.asList("Welcome", "to","Java", "world");
  System.out.println(array1);
  
  /**使用數組*/
  List<String> array2 = Arrays.asList(new String[] {"Welcome", "to","Java", "world"});
  System.out.println(array2);
 }
 
}

 運行上述程序,輸出如下內容。

[Welcome, to, Java, world]
[Welcome, to, Java, world]

心血來潮,突然想在創建的列表中添加一個字符串“Cool~~~”,  走一個。

?
1
2
3
/**使用變長參數*/
 List<String> array1 = Arrays.asList("Welcome", "to","Java", "world");
 array1.add("Cool~~~");

結果,遇到一個UnsupportedOperationException異常

?
1
2
3
4
Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractList.add(Unknown Source)
 at test.ArrayExample.main(ArrayExample.java:36)

不可思議,new ArrayList<>(a)產生的列表調用add方法,竟然遇到問題。

原因查找

那么問題來了,到底發生了什么事情?帶著疑問,去查看一下Arrays.asList中使用的ArrayList到底長啥樣?

原來Arrays的asList方法使用的ArrayList類是一個內部定義的類,而不是java.util.ArrayList類。

其源代碼如下:

?
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
/**
  * @serial include
  */
 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) {
     if (array==null)
       throw new NullPointerException();
     a = array;
   }
 
   public int size() {
     return a.length;
   }
 
   public Object[] toArray() {
     return a.clone();
   }
 
   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;
   }
 
   public E get(int index) {
     return a[index];
   }
 
   public E set(int index, E element) {
     E oldValue = a[index];
     a[index] = element;
     return oldValue;
   }
 
   public int indexOf(Object o) {
     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;
   }
 
   public boolean contains(Object o) {
     return indexOf(o) != -1;
   }
 }

從這個內部類ArrayList的實現可以看出,它繼承了抽象類java.util.AbstractList<E>, 但是沒有重寫add和remove方法,沒有給出具體的實現。

但是,默認情況下,java.util.AbstractList類在add、set以及remove方法中,直接會拋出UnsupportedOperationException異常。AbstractList的部分源代碼如下:

?
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
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
  /**
   * Sole constructor. (For invocation by subclass constructors, typically
   * implicit.)
   */
  protected AbstractList() {
  }
 
  public E set(int index, E element) {
    throw new UnsupportedOperationException();
  }
 
  /**
   * {@inheritDoc}
   *
   * <p>This implementation always throws an
   * {@code UnsupportedOperationException}.
   *
   * @throws UnsupportedOperationException {@inheritDoc}
   * @throws ClassCastException      {@inheritDoc}
   * @throws NullPointerException     {@inheritDoc}
   * @throws IllegalArgumentException   {@inheritDoc}
   * @throws IndexOutOfBoundsException   {@inheritDoc}
   */
  public void add(int index, E element) {
    throw new UnsupportedOperationException();
  }
 
  /**
   * {@inheritDoc}
   *
   * <p>This implementation always throws an
   * {@code UnsupportedOperationException}.
   *
   * @throws UnsupportedOperationException {@inheritDoc}
   * @throws IndexOutOfBoundsException   {@inheritDoc}
   */
  public E remove(int index) {
    throw new UnsupportedOperationException();
  }
}

 正是因為java.util.Arrays類的內部類ArrayList沒有重寫add和remove方法,所以,當我們調用其add方法時,其實就是調用了AbstractList類的add方法,結果就是直接拋出UnsupportedOperationException異常。

同理,在調用remove方法,或者調用與add、remove方法相關聯的其它方法(如addAll)同樣會遇到UnsupportedOperationException異常。

addAll的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用變長參數*/
    List<String> array1 = Arrays.asList("Welcome", "to", "Java", "world");
    array1.addAll(Arrays.asList("AAA", "BBB"));
  }
 
}
?
1
2
3
4
5
6
Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractList.add(Unknown Source)
 at java.util.AbstractCollection.addAll(Unknown Source)
 at test.ArrayExample.main(ArrayExample.java:36)

set的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用變長參數*/
    List<String> array1 = Arrays.asList("Welcome", "to", "Java", "world");
    System.out.println(array1);
    
    //將Java替換成hello
    array1.set(2, "hello");
    System.out.println(array1);
  }
 
}

正是由于Arrays的內部類ArrayList重寫了set方法,所以上述程序能夠正常運行,不會再拋出UnsupportedOperationException異常。

結果如下:

[Welcome, to, Java, world]
[Welcome, to, hello, world] 

使用場景

從上述的例子和簡單分析來看,Arrays工具類提供了一個方法asList, 使用該方法可以將一個變長參數或者數組轉換成List 。

但是,生成的List的長度是固定的;能夠進行修改操作(比如,修改某個位置的元素);不能執行影響長度的操作(如add、remove等操作)。會拋出UnsupportedOperationException異常。

Arrays.asList比較適合那些已經有數組數據或者一些元素,而需要快速構建一個List,只用于讀取操作,而不進行添加或刪除操作的場景。

如果,想要根據已知數組數據,快速獲取一個可進行增刪改查的列表List,一個比較簡單的方法如下:

重新使用java.util.ArrayList包裝一層。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * @author wangmengjun
 *
 */
public class ArrayExample {
 
  public static void main(String[] args) {
 
    /**使用變長參數*/
    List<String> array1 = new ArrayList<>(Arrays.asList("Welcome", "to", "Java", "world"));
    System.out.println(array1);
 
    array1.add("Cool~~~");
    System.out.println(array1);
 
  }
 
}

結果如下:

[Welcome, to, Java, world]
[Welcome, to, Java, world, Cool~~~]

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品亚洲综合久久中文字幕 | 久青草国产在线观看视频 | 久久热这里面只有精品 | 欧美一区二区三区视视频 | a男人的天堂久久a毛片 | 91精品国产99久久 | 黑人异族日本人hd | 亚洲黄色小视频 | 忘忧草在线 | 火影忍者羞羞 | 无删减影视免费观看 | 欧美精品久久久久久久影视 | 高清男的插曲女的 欢迎你老狼 | 骚虎tv| 婷婷在线观看香蕉五月天 | 国产日韩高清一区二区三区 | 福利片免费一区二区三区 | 99精品免费在线 | 成人免费视屏 | 9l国产精品久久久久麻豆 | 日本护士厕所xxx | 狠狠色狠狠色综合日日小蛇 | 成成人看片在线 | 美女扒开粉嫩尿口漫画 | 免费看打屁股视频的软件 | 倩女还魂在线观看完整版免费 | 成年性午夜免费视频网站不卡 | 欧美一区二区三 | 息与子中文字幕完整在线 | 日韩永久在线观看免费视频 | 美女撒尿无遮挡免费中国 | 短篇最污的乱淫伦小说全集 | 四虎海外影院 | 喷奶水榨乳ova动漫无修 | 精品久久久久久亚洲精品 | 给我免费的视频在线观看 | 农村妇女野外牲交一级毛片 | 亚洲国产精品第一区二区三区 | 岛国虐乳紧缚媚药调教 | 啪啪链接 | 狠狠撸在线影院 |