欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Java集合系列(二)ArrayList詳解

ArrayList

紹興網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),紹興網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為紹興上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的紹興做網(wǎng)站的公司定做!

ArrayList 是通過一個數(shù)組來實現(xiàn)的,因此它是在連續(xù)的存儲位置存放對象的引用,只不過它比 Array 更智能,能夠根據(jù)集合長度進行自動擴容。

假設(shè)讓我們來實現(xiàn)一個簡單的能夠自動擴容的數(shù)組,我們最容易想到的點就是:

  1. add()的時候需要判斷當前數(shù)組size+1是否等于此時定義的數(shù)組大小;
  2. 若小于直接添加即可;否則,需要先擴容再進行添加。

實際上,ArrayList的內(nèi)部實現(xiàn)原理也是這樣子,我們可以來研究分析一下ArrayList的源碼

add(E e) 源碼分析

/**
   * Appends the specified element to the end of this list.
   *
   * @param e element to be appended to this list
   * @return <tt>true</tt> (as specified by {@link Collection#add})
   */
  public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // 進行擴容校驗
    elementData[size++] = e;      // 將值添加到數(shù)組后面,并將 size+1
    return true;
  }



  /**
   * The array buffer into which the elements of the ArrayList are stored.
   * The capacity of the ArrayList is the length of this array buffer. Any
   * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
   * will be expanded to DEFAULT_CAPACITY when the first element is added.
   */
  transient Object[] elementData; // non-private to simplify nested class access
  
  private void ensureCapacityInternal(int minCapacity) {
    ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));  // elementData 數(shù)組
  }



  /**
   * Default initial capacity.
   */
  private static final int DEFAULT_CAPACITY = 10;
  
  /**
   * Shared empty array instance used for default sized empty instances. We
   * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
   * first element is added.
   */
  private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

  // 返回最大的 index
  private static int calculateCapacity(Object[] elementData, int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {  // 與空數(shù)組實例對比
      return Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    return minCapacity;
  }



  private void ensureExplicitCapacity(int minCapacity) {
    modCount++;

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
      grow(minCapacity);
  }

擴容調(diào)用方法,實際也就是數(shù)組復(fù)制的過程

/**
   * Increases the capacity to ensure that it can hold at least the
   * number of elements specified by the minimum capacity argument.
   *
   * @param minCapacity the desired minimum capacity
   */
  private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
      newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
      newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
  }

add(int index, E element) 源碼分析

/**
   * Inserts the specified element at the specified position in this
   * list. Shifts the element currently at that position (if any) and
   * any subsequent elements to the right (adds one to their indices).
   *
   * @param index index at which the specified element is to be inserted
   * @param element element to be inserted
   * @throws IndexOutOfBoundsException {@inheritDoc}
   */
  public void add(int index, E element) {
    rangeCheckForAdd(index);  // 校驗index是否超過當前定義的數(shù)組大小范圍,超過則拋出 IndexOutOfBoundsException

    ensureCapacityInternal(size + 1); // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
             size - index);   // 復(fù)制,向后移動
    elementData[index] = element;
    size++;
  }
  

  /**
   * A version of rangeCheck used by add and addAll.
   */
  private void rangeCheckForAdd(int index) {
    if (index > size || index < 0)
      throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  }

從上面的源碼分析可知,擴容和隨機插入元素的消耗比較大,因此在實際開發(fā)中,應(yīng)盡量指定ArrayList大小,減少在隨機插入操作。

優(yōu)缺點

優(yōu)點

  1. 封裝了一個動態(tài)再分配的對象數(shù)組
  2. 使用索引進行隨機訪問效率高

缺陷

  1. 在數(shù)組中增刪一個元素,所有元素都要往后往前移動,效率低下

知識腦圖

Java 集合系列(二)ArrayList詳解

以上所述是小編給大家介紹的Java集合系列ArrayList詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

文章名稱:Java集合系列(二)ArrayList詳解
地址分享:http://www.aaarwkj.com/article38/igsdpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
国产高清亚洲一区亚洲二区| 亚洲av综合日韩精品久久| 蜜桃臀内射一区二区三区| 丰满熟妇久久人妻同堂av| 99热这里只有精品中文| 欧美日韩一区二区黄色| 日韩欧美一区二区大片| 精品亚洲欧美日韩国产| 亚洲成人日韩成人av| 国产美女作爱视频网站| 欧美日韩三级国产在线| 国产亚洲一区二区自拍视频| 欧美精品一区影片在线观看| 深夜视频国产在线观看| 亚洲男人天堂超碰在线| 亚洲免费观看视频一区二区三区| 91午夜福利偷拍视频| 久久产精品一区二区三区日韩| 亚洲av一本岛在线播放| 欧美 成人一区二区三区| 亚洲欧洲日韩综合另类| 国产av剧情精品亚洲| 人人妻人人澡人人揉| 中文字幕亚洲精品熟女少妇| 国产乱码免费一区二区三区| 琪琪精品免费一区二区三区| 色综合视频二区偷拍在线| 亚欧成人永久免费视频| 88国产精品久久久久久| 国产av一区二区三区| 欧美福利免费在线视频| 亚洲最色一区二区三区| 久久久久四虎国产精品| 亚洲日本成人一区二区| 亚洲一区二区婷婷久久| 亚洲亚洲精品av在线动| 亚洲欧美另类不卡专区| 国产无套内射三级视频| 日韩精品中文字幕有码| 国产精品亚洲欧美日韩在线播放 | 伊在人天堂亚洲香蕉精品区|