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

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

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

    容器-數(shù)組-ArrayList

    1. ArrayList概要:
    • 默認(rèn)長(zhǎng)度為10
                     public ArrayList() {
                    this(10);
                     }
    • 底層采用數(shù)組存儲(chǔ)private transient Object[] elementData;
            transient表示數(shù)組elementData不需要通過(guò)serialization序列化傳輸
    • 線程不安全,在多線程場(chǎng)景會(huì)出現(xiàn)問(wèn)題,可以考慮使用Vector或者Collections.synchronizedList同步該容器
    • 查詢效率比較高,比較適用于查詢
    • 擴(kuò)容的長(zhǎng)度為初始長(zhǎng)度的一半,建議初始化的時(shí)候設(shè)置已知的長(zhǎng)度,免得容器自己去擴(kuò)容,浪費(fèi)空間以及效率

    2. ArrayList代碼詳解:

    • 增加元素
      public boolean add(E e) {
        ensureCapacity(size + 1);  // Increments modCount!!
        elementData[size++= e;
        
    return true;
        }
    首先檢查數(shù)組是否已滿,如果滿了就開始擴(kuò)容,擴(kuò)容后的長(zhǎng)度為原長(zhǎng)度的1.5倍。
    /**
         * Increases the capacity of this <tt>ArrayList</tt> instance, if
         * necessary, to ensure that it can hold at least the number of elements
         * specified by the minimum capacity argument.
         *
         * 
    @param   minCapacity   the desired minimum capacity
         
    */
        
    public void ensureCapacity(int minCapacity) {
        modCount
    ++;//modCount表示數(shù)組的操作計(jì)數(shù),用于iterator的時(shí)候與
    expectedMod
    Count比較,防止迭代操作對(duì)add,remove等操作影響迭代操作
      
    int oldCapacity = elementData.length;
        
    if (minCapacity > oldCapacity) {         //新插入元素后的長(zhǎng)度大于老的長(zhǎng)度,數(shù)組開始擴(kuò)容
            Object oldData[] 
    = elementData;
            
    int newCapacity = (oldCapacity * 3)/2 + 1;//新空間為原長(zhǎng)度的1.5倍,等于是擴(kuò)容了50%
                
    if (newCapacity < minCapacity)
            newCapacity 
    = minCapacity;
                
    // minCapacity is usually close to size, so this is a win:
                elementData = Arrays.copyOf(elementData, newCapacity);//把之前的元素拷貝到新的數(shù)組    }
        }

    • 刪除元素:

    /**
         * Removes the element at the specified position in this list.
         * Shifts any subsequent elements to the left (subtracts one from their
         * indices).
         *
         * 
    @param index the index of the element to be removed
         * 
    @return the element that was removed from the list
         * 
    @throws IndexOutOfBoundsException {@inheritDoc}
         
    */
        
    public E remove(int index) {
        RangeCheck(index); //檢查索引是否溢出

        modCount
    ++;        //操作計(jì)數(shù)
        E oldValue 
    = (E) elementData[index];

        
    int numMoved = size - index - 1;
        
    if (numMoved > 0)
            System.arraycopy(elementData, index
    +1, elementData, index,//復(fù)制原數(shù)組制定index+1到length-1的元素到elementData的index的索引位
                     numMoved);
        elementData[
    --size] = null// Let gc do its work//最后一位設(shè)置為null

        
    return oldValue;
        }

     /**
         * Checks if the given index is in range.  If not, throws an appropriate
         * runtime exception.  This method does *not* check if the index is
         * negative: It is always used immediately prior to an array access,
         * which throws an ArrayIndexOutOfBoundsException if index is negative.
         
    */
        
    private void RangeCheck(int index) {
        
    if (index >= size)
            
    throw new IndexOutOfBoundsException(
            
    "Index: "+index+", Size: "+size);
        }

    • 獲取元素:
     /**
         * Returns the element at the specified position in this list.
         *
         * 
    @param  index index of the element to return
         * 
    @return the element at the specified position in this list
         * 
    @throws IndexOutOfBoundsException {@inheritDoc}
         
    */
        
    public E get(int index) {
        RangeCheck(index);

        
    return (E) elementData[index];   //直接獲取數(shù)組的索引位
        }

    posted on 2012-02-20 14:28 陳睿 閱讀(1113) 評(píng)論(0)  編輯  收藏 所屬分類: 高級(jí)

    導(dǎo)航

    <2012年2月>
    2930311234
    567891011
    12131415161718
    19202122232425
    26272829123
    45678910

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 无码精品国产一区二区三区免费 | 亚洲国产美女福利直播秀一区二区 | 曰批全过程免费视频免费看| 亚洲免费二区三区| 久久精品7亚洲午夜a| 美女免费视频一区二区| 四虎永久在线精品免费观看地址| 亚洲精品无码你懂的| 国产成人高清精品免费鸭子| 亚洲精品av无码喷奶水糖心| 国产成人免费永久播放视频平台| 亚洲av无码一区二区三区人妖 | 国偷自产一区二区免费视频| 亚洲AV无码专区亚洲AV伊甸园 | 亚洲AV无码一区二区大桥未久| 在线jlzzjlzz免费播放| 337P日本欧洲亚洲大胆艺术图| 国产成人3p视频免费观看| 美女扒开屁股让男人桶爽免费| 亚洲国产婷婷香蕉久久久久久| www.av在线免费观看| 亚洲VA中文字幕无码一二三区| 免费国产黄网站在线观看视频| 亚洲国产午夜精品理论片| 免费高清资源黄网站在线观看| 免费看一级一级人妻片| 亚洲av午夜成人片精品网站 | 亚洲精品视频免费在线观看| 亚洲三级高清免费| 亚洲Aⅴ无码一区二区二三区软件| 国产精品免费久久| 亚洲国产精品网站久久| 暖暖免费高清日本中文| 日韩精品无码免费专区午夜| 亚洲自偷自拍另类图片二区| 国产资源免费观看| 嫩草在线视频www免费看| 亚洲AV无码精品蜜桃| 亚洲色一色噜一噜噜噜| **一级一级毛片免费观看| 黄网站色视频免费看无下截|