ArrayList婧愮爜鍒嗘瀽

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArrayList婧愮爜鍒嗘瀽相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/rac' title='rac'>rac   绱犳暟   搴忓彿   warning   set   鎸囧畾鍏冪礌   range   鎵撳嵃   turn   

ArrayList鏍稿績婧愮爜

package java.util;

import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;


public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * 榛樿鍒濆瀹归噺澶у皬
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * 绌烘暟缁勶紙鐢ㄤ簬绌哄疄渚嬶級銆?     */
    private static final Object[] EMPTY_ELEMENTDATA = {};

     //鐢ㄤ簬榛樿澶у皬绌哄疄渚嬬殑鍏变韩绌烘暟缁勫疄渚嬨€?      //鎴戜滑鎶婂畠浠嶦MPTY_ELEMENTDATA鏁扮粍涓尯鍒嗗嚭鏉ワ紝浠ョ煡閬撳湪娣诲姞绗竴涓厓绱犳椂瀹归噺闇€瑕佸鍔犲灏戙€?    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    /**
     * 淇濆瓨ArrayList鏁版嵁鐨勬暟缁?     */
    transient Object[] elementData; // non-private to simplify nested class access

    /**
     * ArrayList 鎵€鍖呭惈鐨勫厓绱犱釜鏁?     */
    private int size;

    /**
     * 甯﹀垵濮嬪閲忓弬鏁扮殑鏋勯€犲嚱鏁般€傦紙鐢ㄦ埛鑷繁鎸囧畾瀹归噺锛?     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            //鍒涘缓initialCapacity澶у皬鐨勬暟缁?            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            //鍒涘缓绌烘暟缁?            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

    /**
     *榛樿鏋勯€犲嚱鏁帮紝DEFAULTCAPACITY_EMPTY_ELEMENTDATA 涓?.鍒濆鍖栦负10锛屼篃灏辨槸璇村垵濮嬪叾瀹炴槸绌烘暟缁?褰撴坊鍔犵涓€涓厓绱犵殑鏃跺€欐暟缁勫閲忔墠鍙樻垚10
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    /**
     * 鏋勯€犱竴涓寘鍚寚瀹氶泦鍚堢殑鍏冪礌鐨勫垪琛紝鎸夌収瀹冧滑鐢遍泦鍚堢殑杩唬鍣ㄨ繑鍥炵殑椤哄簭銆?     */
    public ArrayList(Collection<? extends E> c) {
        //
        elementData = c.toArray();
        //濡傛灉鎸囧畾闆嗗悎鍏冪礌涓暟涓嶄负0
        if ((size = elementData.length) != 0) {
            // c.toArray 鍙兘杩斿洖鐨勪笉鏄疧bject绫诲瀷鐨勬暟缁勬墍浠ュ姞涓婁笅闈㈢殑璇彞鐢ㄤ簬鍒ゆ柇锛?            //杩欓噷鐢ㄥ埌浜嗗弽灏勯噷闈㈢殑getClass()鏂规硶
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // 鐢ㄧ┖鏁扮粍浠f浛
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

    /**
     * 淇敼杩欎釜ArrayList瀹炰緥鐨勫閲忔槸鍒楄〃鐨勫綋鍓嶅ぇ灏忋€?搴旂敤绋嬪簭鍙互浣跨敤姝ゆ搷浣滄潵鏈€灏忓寲ArrayList瀹炰緥鐨勫瓨鍌ㄣ€?
     */
    public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }
//涓嬮潰鏄疉rrayList鐨勬墿瀹规満鍒?//ArrayList鐨勬墿瀹规満鍒舵彁楂樹簡鎬ц兘锛屽鏋滄瘡娆″彧鎵╁厖涓€涓紝
//閭d箞棰戠箒鐨勬彃鍏ヤ細瀵艰嚧棰戠箒鐨勬嫹璐濓紝闄嶄綆鎬ц兘锛岃€孉rrayList鐨勬墿瀹规満鍒堕伩鍏嶄簡杩欑鎯呭喌銆?    /**
     * 濡傛湁蹇呰锛屽鍔犳ArrayList瀹炰緥鐨勫閲忥紝浠ョ‘淇濆畠鑷冲皯鑳藉绾冲厓绱犵殑鏁伴噺
     * @param   minCapacity   鎵€闇€鐨勬渶灏忓閲?     */
    public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
   //寰楀埌鏈€灏忔墿瀹归噺
    private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
              // 鑾峰彇榛樿鐨勫閲忓拰浼犲叆鍙傛暟鐨勮緝澶у€?            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }
  //鍒ゆ柇鏄惁闇€瑕佹墿瀹?    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            //璋冪敤grow鏂规硶杩涜鎵╁锛岃皟鐢ㄦ鏂规硶浠h〃宸茬粡寮€濮嬫墿瀹逛簡
            grow(minCapacity);
    }

    /**
     * 瑕佸垎閰嶇殑鏈€澶ф暟缁勫ぇ灏?     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    /**
     * ArrayList鎵╁鐨勬牳蹇冩柟娉曘€?     */
    private void grow(int minCapacity) {
        // oldCapacity涓烘棫瀹归噺锛宯ewCapacity涓烘柊瀹归噺
        int oldCapacity = elementData.length;
        //灏唎ldCapacity 鍙崇Щ涓€浣嶏紝鍏舵晥鏋滅浉褰撲簬oldCapacity /2锛?        //鎴戜滑鐭ラ亾浣嶈繍绠楃殑閫熷害杩滆繙蹇簬鏁撮櫎杩愮畻锛屾暣鍙ヨ繍绠楀紡鐨勭粨鏋滃氨鏄皢鏂板閲忔洿鏂颁负鏃у閲忕殑1.5鍊嶏紝
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //鐒跺悗妫€鏌ユ柊瀹归噺鏄惁澶т簬鏈€灏忛渶瑕佸閲忥紝鑻ヨ繕鏄皬浜庢渶灏忛渶瑕佸閲忥紝閭d箞灏辨妸鏈€灏忛渶瑕佸閲忓綋浣滄暟缁勭殑鏂板閲忥紝
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        //鍐嶆鏌ユ柊瀹归噺鏄惁瓒呭嚭浜咥rrayList鎵€瀹氫箟鐨勬渶澶у閲忥紝
        //鑻ヨ秴鍑轰簡锛屽垯璋冪敤hugeCapacity()鏉ユ瘮杈僲inCapacity鍜?MAX_ARRAY_SIZE锛?        //濡傛灉minCapacity澶т簬MAX_ARRAY_SIZE锛屽垯鏂板閲忓垯涓篒nterger.MAX_VALUE锛屽惁鍒欙紝鏂板閲忓ぇ灏忓垯涓?MAX_ARRAY_SIZE銆?        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);
    }
    //姣旇緝minCapacity鍜?MAX_ARRAY_SIZE
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

    /**
     *杩斿洖姝ゅ垪琛ㄤ腑鐨勫厓绱犳暟銆?
     */
    public int size() {
        return size;
    }

    /**
     * 濡傛灉姝ゅ垪琛ㄤ笉鍖呭惈鍏冪礌锛屽垯杩斿洖 true 銆?     */
    public boolean isEmpty() {
        //娉ㄦ剰=鍜?=鐨勫尯鍒?        return size == 0;
    }

    /**
     * 濡傛灉姝ゅ垪琛ㄥ寘鍚寚瀹氱殑鍏冪礌锛屽垯杩斿洖true 銆?     */
    public boolean contains(Object o) {
        //indexOf()鏂规硶锛氳繑鍥炴鍒楄〃涓寚瀹氬厓绱犵殑棣栨鍑虹幇鐨勭储寮曪紝濡傛灉姝ゅ垪琛ㄤ笉鍖呭惈姝ゅ厓绱狅紝鍒欎负-1 
        return indexOf(o) >= 0;
    }

    /**
     *杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾鍏冪礌鐨勯娆″嚭鐜扮殑绱㈠紩锛屽鏋滄鍒楄〃涓嶅寘鍚鍏冪礌锛屽垯涓?1 
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                //equals()鏂规硶姣旇緝
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

    /**
     * 杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾鍏冪礌鐨勬渶鍚庝竴娆″嚭鐜扮殑绱㈠紩锛屽鏋滄鍒楄〃涓嶅寘鍚厓绱狅紝鍒欒繑鍥?1銆?
     */
    public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size-1; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = size-1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

    /**
     * 杩斿洖姝rrayList瀹炰緥鐨勬祬鎷疯礉銆?锛堝厓绱犳湰韬笉琚鍒躲€傦級 
     */
    public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            //Arrays.copyOf鍔熻兘鏄疄鐜版暟缁勭殑澶嶅埗锛岃繑鍥炲鍒跺悗鐨勬暟缁勩€傚弬鏁版槸琚鍒剁殑鏁扮粍鍜屽鍒剁殑闀垮害
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // 杩欎笉搴旇鍙戠敓锛屽洜涓烘垜浠槸鍙互鍏嬮殕鐨?            throw new InternalError(e);
        }
    }

    /**
     *浠ユ纭殑椤哄簭锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍銆?
     *杩斿洖鐨勬暟缁勫皢鏄€滃畨鍏ㄧ殑鈥濓紝鍥犱负璇ュ垪琛ㄤ笉淇濈暀瀵瑰畠鐨勫紩鐢ㄣ€?锛堟崲鍙ヨ瘽璇达紝杩欎釜鏂规硶蹇呴』鍒嗛厤涓€涓柊鐨勬暟缁勶級銆?     *鍥犳锛岃皟鐢ㄨ€呭彲浠ヨ嚜鐢卞湴淇敼杩斿洖鐨勬暟缁勩€?姝ゆ柟娉曞厖褰撳熀浜庨樀鍒楀拰鍩轰簬闆嗗悎鐨凙PI涔嬮棿鐨勬ˉ姊併€?     */
    public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }

    /**
     * 浠ユ纭殑椤哄簭杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級; 
     *杩斿洖鐨勬暟缁勭殑杩愯鏃剁被鍨嬫槸鎸囧畾鏁扮粍鐨勮繍琛屾椂绫诲瀷銆?濡傛灉鍒楄〃閫傚悎鎸囧畾鐨勬暟缁勶紝鍒欒繑鍥炲叾涓€?
     *鍚﹀垯锛屽皢涓烘寚瀹氭暟缁勭殑杩愯鏃剁被鍨嬪拰姝ゅ垪琛ㄧ殑澶у皬鍒嗛厤涓€涓柊鏁扮粍銆?
     *濡傛灉鍒楄〃閫傜敤浜庢寚瀹氱殑鏁扮粍锛屽叾浣欑┖闂达紙鍗虫暟缁勭殑鍒楄〃鏁伴噺澶氫簬姝ゅ厓绱狅級锛屽垯绱ц窡鍦ㄩ泦鍚堢粨鏉熷悗鐨勬暟缁勪腑鐨勫厓绱犺缃负null 銆?     *锛堣繖浠呭湪璋冪敤鑰呯煡閬撳垪琛ㄤ笉鍖呭惈浠讳綍绌哄厓绱犵殑鎯呭喌涓嬫墠鑳界‘瀹氬垪琛ㄧ殑闀垮害銆傦級 
     */
    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        if (a.length < size)
            // 鏂板缓涓€涓繍琛屾椂绫诲瀷鐨勬暟缁勶紝浣嗘槸ArrayList鏁扮粍鐨勫唴瀹?            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
            //璋冪敤System鎻愪緵鐨刟rraycopy()鏂规硶瀹炵幇鏁扮粍涔嬮棿鐨勫鍒?        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

    // Positional Access Operations

    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

    /**
     * 杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€?     */
    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

    /**
     * 鐢ㄦ寚瀹氱殑鍏冪礌鏇挎崲姝ゅ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€?
     */
    public E set(int index, E element) {
        //瀵筰ndex杩涜鐣岄檺妫€鏌?        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        //杩斿洖鍘熸潵鍦ㄨ繖涓綅缃殑鍏冪礌
        return oldValue;
    }

    /**
     * 灏嗘寚瀹氱殑鍏冪礌杩藉姞鍒版鍒楄〃鐨勬湯灏俱€?
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //杩欓噷鐪嬪埌ArrayList娣诲姞鍏冪礌鐨勫疄璐ㄥ氨鐩稿綋浜庝负鏁扮粍璧嬪€?        elementData[size++] = e;
        return true;
    }

    /**
     * 鍦ㄦ鍒楄〃涓殑鎸囧畾浣嶇疆鎻掑叆鎸囧畾鐨勫厓绱犮€?
     *鍏堣皟鐢?rangeCheckForAdd 瀵筰ndex杩涜鐣岄檺妫€鏌ワ紱鐒跺悗璋冪敤 ensureCapacityInternal 鏂规硶淇濊瘉capacity瓒冲澶э紱
     *鍐嶅皢浠巌ndex寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃紱灏唀lement鎻掑叆index浣嶇疆锛涙渶鍚巗ize鍔?銆?     */
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //arraycopy()杩欎釜瀹炵幇鏁扮粍涔嬮棿澶嶅埗鐨勬柟娉曚竴瀹氳鐪嬩竴涓嬶紝涓嬮潰灏辩敤鍒颁簡arraycopy()鏂规硶瀹炵幇鏁扮粍鑷繁澶嶅埗鑷繁
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

    /**
     * 鍒犻櫎璇ュ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€?灏嗕换浣曞悗缁厓绱犵Щ鍔ㄥ埌宸︿晶锛堜粠鍏剁储寮曚腑鍑忓幓涓€涓厓绱狅級銆?
     */
    public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
      //浠庡垪琛ㄤ腑鍒犻櫎鐨勫厓绱?
        return oldValue;
    }

    /**
     * 浠庡垪琛ㄤ腑鍒犻櫎鎸囧畾鍏冪礌鐨勭涓€涓嚭鐜帮紙濡傛灉瀛樺湪锛夈€?濡傛灉鍒楄〃涓嶅寘鍚鍏冪礌锛屽垯瀹冧笉浼氭洿鏀广€?     *杩斿洖true锛屽鏋滄鍒楄〃鍖呭惈鎸囧畾鐨勫厓绱?     */
    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

    /**
     * 浠庡垪琛ㄤ腑鍒犻櫎鎵€鏈夊厓绱犮€?
     */
    public void clear() {
        modCount++;

        // 鎶婃暟缁勪腑鎵€鏈夌殑鍏冪礌鐨勫€艰涓簄ull
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

    /**
     * 鎸夋寚瀹氶泦鍚堢殑Iterator杩斿洖鐨勯『搴忓皢鎸囧畾闆嗗悎涓殑鎵€鏈夊厓绱犺拷鍔犲埌姝ゅ垪琛ㄧ殑鏈熬銆?     */
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

    /**
     * 灏嗘寚瀹氶泦鍚堜腑鐨勬墍鏈夊厓绱犳彃鍏ュ埌姝ゅ垪琛ㄤ腑锛屼粠鎸囧畾鐨勪綅缃紑濮嬨€?     */
    public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

    /**
     * 浠庢鍒楄〃涓垹闄ゆ墍鏈夌储寮曚负fromIndex 锛堝惈锛夊拰toIndex涔嬮棿鐨勫厓绱犮€?     *灏嗕换浣曞悗缁厓绱犵Щ鍔ㄥ埌宸︿晶锛堝噺灏戝叾绱㈠紩锛夈€?     */
    protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // clear to let GC do its work
        int newSize = size - (toIndex-fromIndex);
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }

    /**
     * 妫€鏌ョ粰瀹氱殑绱㈠紩鏄惁鍦ㄨ寖鍥村唴銆?     */
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    /**
     * add鍜宎ddAll浣跨敤鐨剅angeCheck鐨勪竴涓増鏈?     */
    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    /**
     * 杩斿洖IndexOutOfBoundsException缁嗚妭淇℃伅
     */
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    /**
     * 浠庢鍒楄〃涓垹闄ゆ寚瀹氶泦鍚堜腑鍖呭惈鐨勬墍鏈夊厓绱犮€?
     */
    public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        //濡傛灉姝ゅ垪琛ㄨ淇敼鍒欒繑鍥瀟rue
        return batchRemove(c, false);
    }

    /**
     * 浠呬繚鐣欐鍒楄〃涓寘鍚湪鎸囧畾闆嗗悎涓殑鍏冪礌銆?     *鎹㈠彞璇濊锛屼粠姝ゅ垪琛ㄤ腑鍒犻櫎鍏朵腑涓嶅寘鍚湪鎸囧畾闆嗗悎涓殑鎵€鏈夊厓绱犮€?
     */
    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }


    /**
     * 浠庡垪琛ㄤ腑鐨勬寚瀹氫綅缃紑濮嬶紝杩斿洖鍒楄〃涓殑鍏冪礌锛堟寜姝g‘椤哄簭锛夌殑鍒楄〃杩唬鍣ㄣ€?     *鎸囧畾鐨勭储寮曡〃绀哄垵濮嬭皟鐢ㄥ皢杩斿洖鐨勭涓€涓厓绱犱负next 銆?鍒濆璋冪敤previous灏嗚繑鍥炴寚瀹氱储寮曞噺1鐨勫厓绱犮€?
     *杩斿洖鐨勫垪琛ㄨ凯浠e櫒鏄痜ail-fast 銆?
     */
    public ListIterator<E> listIterator(int index) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException("Index: "+index);
        return new ListItr(index);
    }

    /**
     *杩斿洖鍒楄〃涓殑鍒楄〃杩唬鍣紙鎸夐€傚綋鐨勯『搴忥級銆?
     *杩斿洖鐨勫垪琛ㄨ凯浠e櫒鏄痜ail-fast 銆?     */
    public ListIterator<E> listIterator() {
        return new ListItr(0);
    }

    /**
     *浠ユ纭殑椤哄簭杩斿洖璇ュ垪琛ㄤ腑鐨勫厓绱犵殑杩唬鍣ㄣ€?
     *杩斿洖鐨勮凯浠e櫒鏄痜ail-fast 銆?
     */
    public Iterator<E> iterator() {
        return new Itr();
    }

  

ArrayList婧愮爜鍒嗘瀽

System.arraycopy()鍜孉rrays.copyOf()鏂规硶

銆€銆€閫氳繃涓婇潰婧愮爜鎴戜滑鍙戠幇杩欎袱涓疄鐜版暟缁勫鍒剁殑鏂规硶琚箍娉涗娇鐢ㄨ€屼笖寰堝鍦版柟閮界壒鍒阀濡欍€傛瘮濡備笅闈?font color="red">add(int index, E element)鏂规硶灏卞緢宸у鐨勭敤鍒颁簡arraycopy()鏂规硶璁╂暟缁勮嚜宸卞鍒惰嚜宸卞疄鐜拌index寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃?

    /**
     * 鍦ㄦ鍒楄〃涓殑鎸囧畾浣嶇疆鎻掑叆鎸囧畾鐨勫厓绱犮€?
     *鍏堣皟鐢?rangeCheckForAdd 瀵筰ndex杩涜鐣岄檺妫€鏌ワ紱鐒跺悗璋冪敤 ensureCapacityInternal 鏂规硶淇濊瘉capacity瓒冲澶э紱
     *鍐嶅皢浠巌ndex寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃紱灏唀lement鎻掑叆index浣嶇疆锛涙渶鍚巗ize鍔?銆?     */
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //arraycopy()鏂规硶瀹炵幇鏁扮粍鑷繁澶嶅埗鑷繁
        //elementData:婧愭暟缁?index:婧愭暟缁勪腑鐨勮捣濮嬩綅缃?elementData锛氱洰鏍囨暟缁勶紱index + 1锛氱洰鏍囨暟缁勪腑鐨勮捣濮嬩綅缃紱 size - index锛氳澶嶅埗鐨勬暟缁勫厓绱犵殑鏁伴噺锛?        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = element;
        size++;
    }

鍙堝toArray()鏂规硶涓敤鍒颁簡copyOf()鏂规硶


    /**
     *浠ユ纭殑椤哄簭锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍銆?
     *杩斿洖鐨勬暟缁勫皢鏄€滃畨鍏ㄧ殑鈥濓紝鍥犱负璇ュ垪琛ㄤ笉淇濈暀瀵瑰畠鐨勫紩鐢ㄣ€?锛堟崲鍙ヨ瘽璇达紝杩欎釜鏂规硶蹇呴』鍒嗛厤涓€涓柊鐨勬暟缁勶級銆?     *鍥犳锛岃皟鐢ㄨ€呭彲浠ヨ嚜鐢卞湴淇敼杩斿洖鐨勬暟缁勩€?姝ゆ柟娉曞厖褰撳熀浜庨樀鍒楀拰鍩轰簬闆嗗悎鐨凙PI涔嬮棿鐨勬ˉ姊併€?     */
    public Object[] toArray() {
    //elementData锛氳澶嶅埗鐨勬暟缁勶紱size锛氳澶嶅埗鐨勯暱搴?        return Arrays.copyOf(elementData, size);
    }
涓よ€呰仈绯讳笌鍖哄埆

鑱旂郴锛?/strong>
鐪嬩袱鑰呮簮浠g爜鍙互鍙戠幇copyOf()鍐呴儴璋冪敤浜?code>System.arraycopy()
鏂规硶
鍖哄埆锛?/strong>

  1. arraycopy()闇€瑕佺洰鏍囨暟缁勶紝灏嗗師鏁扮粍鎷疯礉鍒颁綘鑷繁瀹氫箟鐨勬暟缁勯噷锛岃€屼笖鍙互閫夋嫨鎷疯礉鐨勮捣鐐瑰拰闀垮害浠ュ強鏀惧叆鏂版暟缁勪腑鐨勪綅缃?/li>
  2. copyOf()鏄郴缁熻嚜鍔ㄥ湪鍐呴儴鏂板缓涓€涓暟缁勶紝骞惰繑鍥炶鏁扮粍銆?/p>

    //涓嬮潰鏄疉rrayList鐨勬墿瀹规満鍒?//ArrayList鐨勬墿瀹规満鍒舵彁楂樹簡鎬ц兘锛屽鏋滄瘡娆″彧鎵╁厖涓€涓紝 //閭d箞棰戠箒鐨勬彃鍏ヤ細瀵艰嚧棰戠箒鐨勬嫹璐濓紝闄嶄綆鎬ц兘锛岃€孉rrayList鐨勬墿瀹规満鍒堕伩鍏嶄簡杩欑鎯呭喌銆? /** * 濡傛湁蹇呰锛屽鍔犳ArrayList瀹炰緥鐨勫閲忥紝浠ョ‘淇濆畠鑷冲皯鑳藉绾冲厓绱犵殑鏁伴噺 * @param minCapacity 鎵€闇€鐨勬渶灏忓閲? */ public void ensureCapacity(int minCapacity) { int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } //寰楀埌鏈€灏忔墿瀹归噺 private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // 鑾峰彇榛樿鐨勫閲忓拰浼犲叆鍙傛暟鐨勮緝澶у€? minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } //鍒ゆ柇鏄惁闇€瑕佹墿瀹?涓婇潰涓や釜鏂规硶閮借璋冪敤 private void ensureExplicitCapacity(int minCapacity) { modCount++; // 濡傛灉璇磎inCapacity涔熷氨鏄墍闇€鐨勬渶灏忓閲忓ぇ浜庝繚瀛楢rrayList鏁版嵁鐨勬暟缁勭殑闀垮害鐨勮瘽锛屽氨闇€瑕佽皟鐢╣row(minCapacity)鏂规硶鎵╁銆? //杩欎釜minCapacity鍒板簳涓哄灏戝憿锛熶妇涓緥瀛愬湪娣诲姞鍏冪礌(add)鏂规硶涓繖涓猰inCapacity鐨勫ぇ灏忓氨涓虹幇鍦ㄦ暟缁勭殑闀垮害鍔? if (minCapacity - elementData.length > 0) //璋冪敤grow鏂规硶杩涜鎵╁锛岃皟鐢ㄦ鏂规硶浠h〃宸茬粡寮€濮嬫墿瀹逛簡 grow(minCapacity); }
        /**
         * ArrayList鎵╁鐨勬牳蹇冩柟娉曘€?     */
        private void grow(int minCapacity) {
           //elementData涓轰繚瀛楢rrayList鏁版嵁鐨勬暟缁?       ///elementData.length姹傛暟缁勯暱搴lementData.size鏄眰鏁扮粍涓殑鍏冪礌涓暟
            // oldCapacity涓烘棫瀹归噺锛宯ewCapacity涓烘柊瀹归噺
            int oldCapacity = elementData.length;
            //灏唎ldCapacity 鍙崇Щ涓€浣嶏紝鍏舵晥鏋滅浉褰撲簬oldCapacity /2锛?        //鎴戜滑鐭ラ亾浣嶈繍绠楃殑閫熷害杩滆繙蹇簬鏁撮櫎杩愮畻锛屾暣鍙ヨ繍绠楀紡鐨勭粨鏋滃氨鏄皢鏂板閲忔洿鏂颁负鏃у閲忕殑1.5鍊嶏紝
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            //鐒跺悗妫€鏌ユ柊瀹归噺鏄惁澶т簬鏈€灏忛渶瑕佸閲忥紝鑻ヨ繕鏄皬浜庢渶灏忛渶瑕佸閲忥紝閭d箞灏辨妸鏈€灏忛渶瑕佸閲忓綋浣滄暟缁勭殑鏂板閲忥紝
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            //鍐嶆鏌ユ柊瀹归噺鏄惁瓒呭嚭浜咥rrayList鎵€瀹氫箟鐨勬渶澶у閲忥紝
            //鑻ヨ秴鍑轰簡锛屽垯璋冪敤hugeCapacity()鏉ユ瘮杈僲inCapacity鍜?MAX_ARRAY_SIZE锛?        //濡傛灉minCapacity澶т簬MAX_ARRAY_SIZE锛屽垯鏂板閲忓垯涓篒nterger.MAX_VALUE锛屽惁鍒欙紝鏂板閲忓ぇ灏忓垯涓?MAX_ARRAY_SIZE銆?        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);
        }
        

    銆€銆€鎵╁鏈哄埗浠g爜宸茬粡鍋氫簡璇︾粏鐨勮В閲娿€傚彟澶栧€煎緱娉ㄦ剰鐨勬槸澶у寰堝鏄撳拷鐣ョ殑涓€涓繍绠楃锛?strong>绉讳綅杩愮畻绗?/strong>
    銆€銆€绠€浠?/strong>锛氱Щ浣嶈繍绠楃灏辨槸鍦ㄤ簩杩涘埗鐨勫熀纭€涓婂鏁板瓧杩涜骞崇Щ銆傛寜鐓у钩绉荤殑鏂瑰悜鍜屽~鍏呮暟瀛楃殑瑙勫垯鍒嗕负涓夌:<<(宸︾Щ)銆?font color="red">>>(甯︾鍙峰彸绉?鍜?font color="red">>>>(鏃犵鍙峰彸绉?銆?br /> 銆€銆€浣滅敤锛?strong>瀵逛簬澶ф暟鎹殑2杩涘埗杩愮畻,浣嶇Щ杩愮畻绗︽瘮閭d簺鏅€氳繍绠楃鐨勮繍绠楄蹇緢澶?鍥犱负绋嬪簭浠呬粎绉诲姩涓€涓嬭€屽凡,涓嶅幓璁$畻,杩欐牱鎻愰珮浜嗘晥鐜?鑺傜渷浜嗚祫婧?/strong>
    銆€銆€姣斿杩欓噷锛歩nt newCapacity = oldCapacity + (oldCapacity >> 1);
    鍙崇Щ涓€浣嶇浉褰撲簬闄?锛屽彸绉籲浣嶇浉褰撲簬闄や互 2 鐨?n 娆℃柟銆傝繖閲?oldCapacity 鏄庢樉鍙崇Щ浜?浣嶆墍浠ョ浉褰撲簬oldCapacity /2銆?/p>

    鍙﹀闇€瑕佹敞鎰忕殑鏄細

    1. java 涓殑length 灞炴€?/strong>鏄拡瀵规暟缁勮鐨?姣斿璇翠綘澹版槑浜嗕竴涓暟缁?鎯崇煡閬撹繖涓暟缁勭殑闀垮害鍒欑敤鍒颁簡 length 杩欎釜灞炴€?

    2. java 涓殑length()鏂规硶鏄拡瀵瑰瓧 绗︿覆String璇寸殑,濡傛灉鎯崇湅杩欎釜瀛楃涓茬殑闀垮害鍒欑敤鍒?length()杩欎釜鏂规硶.

    3. .java 涓殑size()鏂规硶鏄拡瀵规硾鍨嬮泦鍚堣鐨?濡傛灉鎯崇湅杩欎釜娉涘瀷鏈夊灏戜釜鍏冪礌,灏辫皟鐢ㄦ鏂规硶鏉ユ煡鐪?

    (1)private class Itr implements Iterator<E> (2)private class ListItr extends Itr implements ListIterator<E> (3)private class SubList extends AbstractList<E> implements RandomAccess (4)static final class ArrayListSpliterator<E> implements Spliterator<E>

    銆€銆€ArrayList鏈夊洓涓唴閮ㄧ被锛屽叾涓殑Itr鏄疄鐜颁簡Iterator鎺ュ彛锛屽悓鏃堕噸鍐欎簡閲岄潰鐨?strong>hasNext()锛?next()锛?remove() 绛夋柟娉曪紱鍏朵腑鐨?strong>ListItr 缁ф壙 Itr锛屽疄鐜颁簡ListIterator鎺ュ彛锛屽悓鏃堕噸鍐欎簡hasPrevious()锛?nextIndex()锛?previousIndex()锛?previous()锛?set(E e)锛?add(E e) 绛夋柟娉曪紝鎵€浠ヨ繖涔熷彲浠ョ湅鍑轰簡 Iterator鍜孡istIterator鐨勫尯鍒? ListIterator鍦↖terator鐨勫熀纭€涓婂鍔犱簡娣诲姞瀵硅薄锛屼慨鏀瑰璞★紝閫嗗悜閬嶅巻绛夋柟娉曪紝杩欎簺鏄疘terator涓嶈兘瀹炵幇鐨勩€?/p>

    ArrayList缁忓吀Demo

    package list;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class ArrayListDemo {
    
        public static void main(String[] srgs){
             ArrayList<Integer> arrayList = new ArrayList<Integer>();
    
             System.out.printf("Before add:arrayList.size() = %d
    ",arrayList.size());
    
             arrayList.add(1);
             arrayList.add(3);
             arrayList.add(5);
             arrayList.add(7);
             arrayList.add(9);
             System.out.printf("After add:arrayList.size() = %d
    ",arrayList.size());
    
             System.out.println("Printing elements of arrayList");
             // 涓夌閬嶅巻鏂瑰紡鎵撳嵃鍏冪礌
             // 绗竴绉嶏細閫氳繃杩唬鍣ㄩ亶鍘?         System.out.print("閫氳繃杩唬鍣ㄩ亶鍘?");
             Iterator<Integer> it = arrayList.iterator();
             while(it.hasNext()){
                 System.out.print(it.next() + " ");
             }
             System.out.println();
    
             // 绗簩绉嶏細閫氳繃绱㈠紩鍊奸亶鍘?         System.out.print("閫氳繃绱㈠紩鍊奸亶鍘?");
             for(int i = 0; i < arrayList.size(); i++){
                 System.out.print(arrayList.get(i) + " ");
             }
             System.out.println();
    
             // 绗笁绉嶏細for寰幆閬嶅巻
             System.out.print("for寰幆閬嶅巻:");
             for(Integer number : arrayList){
                 System.out.print(number + " ");
             }
    
             // toArray鐢ㄦ硶
             // 绗竴绉嶆柟寮?鏈€甯哥敤)
             Integer[] integer = arrayList.toArray(new Integer[0]);
    
             // 绗簩绉嶆柟寮?瀹规槗鐞嗚В)
             Integer[] integer1 = new Integer[arrayList.size()];
             arrayList.toArray(integer1);
    
             // 鎶涘嚭寮傚父锛宩ava涓嶆敮鎸佸悜涓嬭浆鍨?         //Integer[] integer2 = new Integer[arrayList.size()];
             //integer2 = arrayList.toArray();
             System.out.println();
    
             // 鍦ㄦ寚瀹氫綅缃坊鍔犲厓绱?         arrayList.add(2,2);
             // 鍒犻櫎鎸囧畾浣嶇疆涓婄殑鍏冪礌
             arrayList.remove(2);    
             // 鍒犻櫎鎸囧畾鍏冪礌
             arrayList.remove((Object)3);
             // 鍒ゆ柇arrayList鏄惁鍖呭惈5
             System.out.println("ArrayList contains 5 is: " + arrayList.contains(5));
    
             // 娓呯┖ArrayList
             arrayList.clear();
             // 鍒ゆ柇ArrayList鏄惁涓虹┖
             System.out.println("ArrayList is empty: " + arrayList.isEmpty());
        }
    }

以上是关于ArrayList婧愮爜鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章

JDK婧愮爜鍒嗘瀽鍒濇鏁寸悊

Memcached婧愮爜鍒嗘瀽涔媔tems.c

log4j2婧愮爜鍒嗘瀽-1

婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>

绮惧敖 MyBatis 婧愮爜鍒嗘瀽 - SqlSession 浼氳瘽涓?SQL 鎵ц鍏ュ彛

鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽