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

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

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

    容器-數(shù)組-ArrayList

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

    2. ArrayList代碼詳解:

    • 增加元素
      public boolean add(E e) {
        ensureCapacity(size + 1);  // Increments modCount!!
        elementData[size++= e;
        
    return true;
        }
    首先檢查數(shù)組是否已滿,如果滿了就開始擴容,擴容后的長度為原長度的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ù)組的操作計數(shù),用于iterator的時候與
    expectedMod
    Count比較,防止迭代操作對add,remove等操作影響迭代操作
      
    int oldCapacity = elementData.length;
        
    if (minCapacity > oldCapacity) {         //新插入元素后的長度大于老的長度,數(shù)組開始擴容
            Object oldData[] 
    = elementData;
            
    int newCapacity = (oldCapacity * 3)/2 + 1;//新空間為原長度的1.5倍,等于是擴容了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
    ++;        //操作計數(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 陳睿 閱讀(1105) 評論(0)  編輯  收藏 所屬分類: 高級

    導航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統(tǒng)計

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产精品成人综合色在线| 亚洲国产成人精品激情| 亚洲xxxxxx| 少妇无码一区二区三区免费| 中文字幕亚洲综合久久菠萝蜜| 亚洲伊人久久综合中文成人网| 亚洲一区精品伊人久久伊人| 成人A毛片免费观看网站| 国产成人亚洲精品91专区手机| 亚洲精品伊人久久久久| 免费二级毛片免费完整视频| 亚洲成a人片在线不卡一二三区| 成年人网站免费视频| 亚洲综合丁香婷婷六月香| 日批日出水久久亚洲精品tv| jizz免费在线观看| 亚洲毛片av日韩av无码| 国产午夜无码片免费| 日韩亚洲国产高清免费视频| 国产精品免费看久久久无码| 日本免费精品一区二区三区| 亚洲男人的天堂www| 国产精品免费AV片在线观看| 亚洲综合激情九月婷婷| 最近最好的中文字幕2019免费| 婷婷亚洲综合五月天小说 | 一级毛片免费播放试看60分钟| 2021在线永久免费视频| 亚洲精品白色在线发布| 精品国产亚洲男女在线线电影 | 扒开双腿猛进入爽爽免费视频 | 亚洲av日韩av欧v在线天堂| 免费观看无遮挡www的小视频| 亚洲中文字幕无码一区二区三区 | 国产成人高清亚洲一区91| 免费国产a国产片高清网站| 免费观看国产网址你懂的| 国产一区二区三区免费观在线| 亚洲国产精久久久久久久| 9久9久女女免费精品视频在线观看 | 免费看a级黄色片|