<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tbwshc

    Arrays.asList方法 學(xué)習(xí)記錄

    Arrays工具類提供了一些比較實(shí)用的方法,比如sort, binarySearch, fill等。其中還有一個asList方法,此方法能夠?qū)⒁粋€變長參數(shù)或者數(shù)組轉(zhuǎn)換成List。
      但是,這個生成的List,它是固定長度的,如果對其進(jìn)行add或者remove的操作,會拋出UnsupportedOperationException,為什么會這樣呢?
      帶著疑問,查看一下Arrays的源碼,可以得到問題的結(jié)果。
      Java代碼
      /**
      * 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 Collection.toArray. The returned list is
      * serializable and implements {@link RandomAccess}.
      *
      *
      This method also provides a convenient way to create a fixed-size
      * list initialized to contain several elements:
      *
      * List stooges = Arrays.asList("Larry", "Moe", "Curly");
      *
      *
      * @param a the array by which the list will be backed.
      * @return a list view of the specified array.
      * @see Collection#toArray()
      */
      public static List asList(T... a) {
      return new ArrayList(a);

      方法asList返回的是new ArrayList(a)。但是,這個ArrayList并不是java.util.ArrayList,它是一個Arrays類中的重新定義的內(nèi)部類。
      具體的實(shí)現(xiàn)如下:
      Java代碼
      /**
      * @serial include
      */
      private static class ArrayList extends AbstractList
      implements RandomAccess, java.io.Serializable
      {
      private static final long serialVersionUID = -2764017481108945198L;
      private Object[] a;
      ArrayList(E[] array) {
      if (array==null)
      throw new NullPointerException();
      a = array;
      }
      public int size() {
      return a.length;
      }
      public Object[] toArray() {
      return (Object[])a.clone();
      }
      public E get(int index) {
      return (E)a[index];
      }
      public E set(int index, E element) {
      Object oldValue = a[index];
      a[index] = element;
      return (E)oldValue;
      }
      public int indexOf(Object o) {
      if (o==null) {
      for (int i=0; i
      if (a[i]==null)
      return i;
      } else {
      for (int i=0; i
      if (o.equals(a[i]))
      return i;
      }
      return -1;
      }
      public boolean contains(Object o) {
      return indexOf(o) != -1;
      }
      }
      從這個內(nèi)部類ArrayList的實(shí)現(xiàn)可以看出,它繼承了類AbstractList,但是沒有重寫add和remove方法,沒有給出具體的實(shí)現(xiàn)。查看一下AbstractList類中對add和remove方法的定義,如果一個list不支持add和remove就會拋出UnsupportedOperationException。
      Java代碼
      public abstract class AbstractList extends AbstractCollection implements List {
      /**
      * Sole constructor. (For invocation by subclass constructors, typically
      * implicit.)
      */
      protected AbstractList() {
      }
      /**
      * Appends the specified element to the end of this List (optional
      * operation).
      *
      * This implementation calls add(size(), o).
      *
      * Note that this implementation throws an
      * UnsupportedOperationException unless add(int, Object)
      * is overridden.
      *
      * @param o element to be appended to this list.
      *
      * @return true (as per the general contract of
      * Collection.add).
      *
      * @throws UnsupportedOperationException if the add method is not
      * supported by this Set.
      *
      * @throws ClassCastException if the class of the specified element
      * prevents it from being added to this set.
      *
      * @throws IllegalArgumentException some aspect of this element prevents
      * it from being added to this collection.
      */
      public boolean add(E o) {
      add(size(), o);
      return true;
      }
      /**
      * Inserts the specified element at the specified position in this list
      * (optional operation). Shifts the element currently at that position
      * (if any) and any subsequent elements to the right (adds one to their
      * indices).
      *
      * This implementation always throws an UnsupportedOperationException.
      *
      * @param index index at which the specified element is to be inserted.
      * @param element element to be inserted.
      *
      * @throws UnsupportedOperationException if the add method is not
      * supported by this list.
      * @throws ClassCastException if the class of the specified element
      * prevents it from being added to this list.
      * @throws IllegalArgumentException if some aspect of the specified
      * element prevents it from being added to this list.
      * @throws IndexOutOfBoundsException index is out of range (index <
      * 0 || index > size()).
      */
      public void add(int index, E element) {
      throw new UnsupportedOperationException();
      }
      /**
      * Removes the element at the specified position in this list (optional
      * operation). Shifts any subsequent elements to the left (subtracts one
      * from their indices). Returns the element that was removed from the
      * list.
      *
      * This implementation always throws an
      * UnsupportedOperationException.
      *
      * @param index the index of the element to remove.
      * @return the element previously at the specified position.
      *
      * @throws UnsupportedOperationException if the remove method is
      * not supported by this list.
      * @throws IndexOutOfBoundsException if the specified index is out of
      * range (index < 0 || index >= size()).
      */
      public E remove(int index) {
      throw new UnsupportedOperationException();
      }
      }
      至此,為什么Arrays.asList產(chǎn)生的List是不可添加或者刪除,否則會產(chǎn)生UnsupportedOperationException,就可以得到解釋了。
      如果我們想把一個變長或者數(shù)據(jù)轉(zhuǎn)變成List, 而且tb期望這個List能夠進(jìn)行add或者remove操作,那該怎么做呢?
      我們可以寫一個類似的方法,里面直接采用java.util.ArrayList即可。
      比如:
      Java代碼
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      public class MyArrays {
      public static List asList(T... a) {
      List list = new ArrayList();
      Collections.addAll(list, a);
      return list;
      }
      }
      測試代碼如下:
      Java代碼
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      public class Test {
      @SuppressWarnings("unchecked")
      public static void main(String[] args) {
      List stooges = Arrays.asList("Larry", "Moe", "Curly");
      print(stooges);
      List> seasonsList = Arrays.asList(retrieveSeasonsList());
      print(seasonsList);
      /*
      * 自己實(shí)現(xiàn)一個asList方法,能夠添加和刪除。
      */
      List list = MyArrays.asList("Larry", "Moe", "Curly");
      list.add("Hello");
      print(list);
      }
      private static void print(List list) {
      System.out.println(list);
      }
      private static List retrieveSeasonsList() {
      List seasonsList = new ArrayList();
      seasonsList.add("Spring");
      seasonsList.add("Summer");
      seasonsList.add("Autumn");
      seasonsList.add("Winter");
      return seasonsList;
      }
      }
      輸出結(jié)果:
      [Larry, Moe, Curly]
      [[Spring, Summer, Autumn, Winter]]
      [Larry, Moe, Curly, Hello]

    posted on 2013-07-15 17:08 chen11-1 閱讀(236) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 精品亚洲成A人无码成A在线观看| 免费永久在线观看黄网站| 国产成人亚洲综合无码| 国产亚洲一卡2卡3卡4卡新区| 91情侣在线精品国产免费| 亚洲国产精品免费在线观看| 97青青草原国产免费观看| 亚洲视频在线视频| 99精品视频在线观看免费专区| 久久精品国产99精品国产亚洲性色| 日本牲交大片免费观看| 亚洲中文字幕一二三四区| 成年女性特黄午夜视频免费看| 亚洲最大福利视频| 免费看片免费播放| 乱人伦中文视频在线观看免费| 亚洲精品国自产拍在线观看| j8又粗又长又硬又爽免费视频| 久久国产精品免费一区| 日韩精品在线免费观看| 久久久久久亚洲av成人无码国产| 三年片在线观看免费观看大全动漫| 亚洲av日韩av不卡在线观看| 69av免费视频| 精品亚洲福利一区二区| 国产精品亚洲w码日韩中文| 久久久久成人片免费观看蜜芽| 亚洲激情电影在线| 特级无码毛片免费视频尤物 | 亚洲激情校园春色| 日本免费一区二区三区最新vr| 一级做a爰片久久毛片免费陪| 亚洲春色在线视频| 久久久久久国产a免费观看黄色大片| 亚洲日韩国产AV无码无码精品| 亚洲区不卡顿区在线观看| 美女被cao网站免费看在线看| 在线观看亚洲AV日韩A∨| 亚洲熟伦熟女新五十路熟妇 | 亚洲精品亚洲人成在线麻豆| 免费无码一区二区三区蜜桃大|