闆嗗悎ArrayList鍒嗘瀽

Posted

tags:

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

鏍囩锛?a href='http://www.mamicode.com/so/1/iter' title='iter'>iter   capacity   鏈哄埗   rac   math   tor   vat   ali   

ArrayList

鎻忚堪

  • 缁ф壙AbstractList绫伙紝瀹炵幇List鎺ュ彛
  • 瀹炵幇浜哛andmoAccess鎺ュ彛锛屽嵆鎻愪緵浜嗛殢鏈鸿闂姛鑳?/li>
  • 瀹炵幇浜咰loneable鎺ュ彛锛屽嵆瑕嗙洊浜嗗嚱鏁癱lone()锛岃兘琚厠闅嗐€?/li>
  • 瀹炵幇java.io.Serializable鎺ュ彛锛岃繖鎰忓懗鐫€ArrayList鏀寔搴忓垪鍖栵紝鑳介€氳繃搴忓垪鍖栧幓浼犺緭銆?/li>
  • 鍩轰簬娉涘瀷鍔ㄦ€佹暟缁?List)鎵╁(纰板埌鏁扮粍鍏堟兂鍒拌繛缁殑鍐呭瓨绌洪棿锛屾晠绌洪棿鏁堢巼涓嶉珮)
  • ArrayList鍙互浠(1)鐨勬椂闂村鏉傚害鍘绘牴鎹笅鏍囪闂厓绱犮€?鏃堕棿鏁堢巼寰堥珮)
  • 绾跨▼涓嶅畨鍏?/strong>鐨勶紝鍏佽鍏冪礌涓簄ull

閬嶅巻浣跨敤

浣跨敤for寰幆锛岄€氳繃绱㈠紩闅忔満璁块棶鏁堢巼鏈€楂橈紝鍏舵鏄痜oreach寰幆锛屾渶鎱㈡槸杩唬鍣ㄥ惊鐜?/p>

foreach 鏈€缁堜細琚浆鎹㈡垚杩唬鍣ㄩ亶鍘嗙殑褰㈠紡锛屾墍浠ユ槸涓嶅for寰幆鐨?/p>

涓嶤ollection鍏崇郴

鎶€鏈浘鐗? src=

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable // 搴忓垪鍖杋d private static final long serialVersionUID = 8683452581122892189L; // 榛樿鍒濆鐨勫閲? private static final int DEFAULT_CAPACITY = 10; // 涓€涓┖瀵硅薄 private static final Object[] EMPTY_ELEMENTDATA = new Object[0]; // 涓€涓┖瀵硅薄锛屽鏋滀娇鐢ㄩ粯璁ゆ瀯閫犲嚱鏁板垱寤猴紝鍒欓粯璁ゅ璞″唴瀹归粯璁ゆ槸璇ュ€? private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0]; // 褰撳墠鏁版嵁瀵硅薄瀛樻斁鍦版柟锛屽綋鍓嶅璞′笉鍙備笌搴忓垪鍖? transient Object[] elementData; // 褰撳墠鏁扮粍闀垮害 private int size; // 鏁扮粍鏈€澶ч暱搴? private static final int MAX_ARRAY_SIZE = 2147483639;

transient鍏抽敭瀛楄В鏋?/h3>

Java涓璽ransient鍏抽敭瀛楃殑浣滅敤锛岀畝鍗曞湴璇达紝灏辨槸璁╂煇浜涜淇グ鐨勬垚鍛樺睘鎬у彉閲忎笉琚簭鍒楀寲銆?/p>

鏈変簡transient鍏抽敭瀛楀0鏄庯紝鍒欒繖涓彉閲忎笉浼氬弬涓庡簭鍒楀寲鎿嶄綔锛屽嵆浣挎墍鍦ㄧ被瀹炵幇浜哠erializable鎺ュ彛锛屽弽搴忓垪鍖栧悗璇ュ彉閲忎负绌哄€笺€?/p>

閭d箞闂鏉ヤ簡锛欰rrayList涓暟缁勫0鏄庯細transient Object[] elementData;锛屼簨瀹炰笂鎴戜滑浣跨敤ArrayList鍦ㄧ綉缁滀紶杈撶敤鐨勫緢姝e父锛屽苟娌℃湁鍑虹幇绌哄€笺€?/p>

鍘熸潵锛?code>ArrayList鍦ㄥ簭鍒楀寲鐨勬椂鍊欎細璋冪敤writeObject()鏂规硶锛屽皢size鍜?code>element鍐欏叆ObjectOutputStream锛涘弽搴忓垪鍖栨椂璋冪敤readObject()锛屼粠ObjectInputStream鑾峰彇size鍜?code>element锛屽啀鎭㈠鍒?code>elementData銆?/p>

閭d负浠€涔堜笉鐩存帴鐢╡lementData鏉ュ簭鍒楀寲锛岃€岄噰鐢ㄤ笂璇夌殑鏂瑰紡鏉ュ疄鐜板簭鍒楀寲鍛紵

鍘熷洜鍦ㄤ簬elementData鏄竴涓紦瀛樻暟缁勶紝瀹冮€氬父浼氶鐣欎竴浜涘閲忥紝绛夊閲忎笉瓒虫椂鍐嶆墿鍏呭閲忥紝閭d箞鏈変簺绌洪棿鍙兘灏辨病鏈夊疄闄呭瓨鍌ㄥ厓绱狅紝閲囩敤涓婅瘔鐨勬柟寮忔潵瀹炵幇搴忓垪鍖栨椂锛屽氨鍙互淇濊瘉鍙簭鍒楀寲瀹為檯瀛樺偍鐨勯偅浜涘厓绱狅紝鑰屼笉鏄暣涓暟缁勶紝浠庤€?strong>鑺傜渷绌洪棿鍜屾椂闂?/strong>銆?/p>

鎵€浠rrayList鐨勮璁¤€呭皢elementData璁捐涓簍ransient锛岀劧鍚庡湪writeObject鏂规硶涓墜鍔ㄥ皢鍏跺簭鍒楀寲锛屽苟涓斿彧搴忓垪鍖栦簡瀹為檯瀛樺偍鐨勯偅浜涘厓绱狅紝鑰屼笉鏄暣涓暟缁勩€?/p>

瑙佹簮鐮侊細

// Write out all elements in the proper order.for (int i=0; i<size; i++)     s.writeObject(elementData[i]);

浠庢簮鐮佷腑锛屽彲浠ヨ瀵熷埌 寰幆鏃舵槸浣跨敤i<size鑰屼笉鏄?i<elementData.length锛岃鏄庡簭鍒楀寲鏃讹紝鍙渶瀹為檯瀛樺偍鐨勯偅浜涘厓绱狅紝鑰屼笉鏄暣涓暟缁勩€?/p>

鏃犲弬鏋勯€?/h3>
public ArrayList() 
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;

榛樿鏃犲弬鏋勫缓鏂规硶鍒涘缓ArrayList瀵硅薄

new ArrayList() 鍒涘缓瀵硅薄锛屾暟缁勯暱搴ize=0锛岃€宔lementData鏁扮粍鐨勯暱搴︿负size+1锛?/p>

绗竴娆dd鏃讹紝elementData浼氬彉鎴愰粯璁ら暱搴?10(DEFAULT_CAPACITY)

public ArrayList(int initialCapacity) if (initialCapacity > 0) this.elementData = new Object[initialCapacity]; else if (initialCapacity == 0) this.elementData = EMPTY_ELEMENTDATA; else throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);

initialCapacity鎸囧畾鍒濆鏁扮粍闀垮害銆?/p>

public ArrayList(Collection<? extends E> c) elementData = c.toArray(); if ((size = elementData.length) != 0) // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); else // replace with empty array. this.elementData = EMPTY_ELEMENTDATA;
  • 灏哻ollection瀵硅薄杞崲鎴愭暟缁勶紝鐒跺悗灏嗘暟缁勭殑鍦板潃璧嬬粰elementData銆?/li>
  • 鏇存柊size鐨勫€硷紝鍚屾椂鍒ゆ柇size鐨勫ぇ灏忥紝濡傛灉鏄痵ize绛変簬0锛岀洿鎺ュ皢绌哄璞MPTY_ELEMENTDATA鐨勫湴鍧€璧嬬粰elementData
  • 濡傛灉size鐨勫€煎ぇ浜?锛屽垯鎵цArrays.copyOf鏂规硶锛屾妸collection瀵硅薄鐨勫唴瀹癸紙鍙互鐞嗚В涓烘繁鎷疯礉锛塩opy鍒癳lementData涓€?/li>

娉ㄦ剰锛歵his.elementData = arg0.toArray(); 杩欓噷鎵ц鐨勭畝鍗曡祴鍊兼槸娴呮嫹璐濓紝鎵€浠ヨ鎵цArrays.copyOf鍋氭繁鎷疯礉

add(E e)鏂规硶

  /**
     *澧炲姞鎸囧畾鐨勫厓绱犲埌ArrayList鐨勬渶鍚庝綅缃?     * @param e 瑕佹坊鍔犵殑鍏冪礌
     * @return
     */
    public boolean add(E e) 
        // 纭畾ArrayList鐨勫閲忓ぇ灏?--涓ヨ皑
        // 娉ㄦ剰锛歴ize + 1锛屼繚璇佽祫婧愮┖闂翠笉琚氮璐癸紝
        // 鈽嗏槅鈽嗘寜褰撳墠鎯呭喌锛屼繚璇佽瀛樺灏戜釜鍏冪礌锛屽氨鍙垎閰嶅灏戠┖闂磋祫婧?        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    
  /**
     * 绉佹湁鏂规硶锛氭槑纭?ArrayList 鐨勫閲忥紝鎻愪緵缁欐湰绫讳娇鐢ㄧ殑鏂规硶
     * - 鐢ㄤ簬鍐呴儴浼樺寲锛屼繚璇佺┖闂磋祫婧愪笉琚氮璐癸細灏ゅ叾鍦?add() 鏂规硶娣诲姞鏃惰捣鏁?     * @param minCapacity    鎸囧畾鐨勬渶灏忓閲?     */
    private void ensureCapacityInternal(int minCapacity) 
        // 鑻?elementData == 锛屽垯鍙?minCapacity 涓?榛樿瀹归噺鍜屽弬鏁?minCapacity 涔嬮棿鐨勬渶澶у€?        // 娉細ensureCapacity() 鏄彁渚涚粰鐢ㄦ埛浣跨敤鐨勬柟娉曪紝鍦?ArrayList 鐨勫疄鐜颁腑骞舵病鏈変娇鐢?        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) 
            minCapacity= Math.max(DEFAULT_CAPACITY, minCapacity);
        
        ensureExplicitCapacity(minCapacity);
    
    /**
     * 绉佹湁鏂规硶锛氭槑纭?ArrayList 鐨勫閲?     * - 鐢ㄤ簬鍐呴儴浼樺寲锛屼繚璇佺┖闂磋祫婧愪笉琚氮璐癸細灏ゅ叾鍦?add() 鏂规硶娣诲姞鏃惰捣鏁?     * @param minCapacity    鎸囧畾鐨勬渶灏忓閲?     */
    private void ensureExplicitCapacity(int minCapacity) 
        // 灏嗏€滀慨鏀圭粺璁℃暟鈥?1锛岃鍙橀噺涓昏鏄敤鏉ュ疄鐜癴ail-fast鏈哄埗鐨?        modCount++;
        // 闃叉婧㈠嚭浠g爜锛氱‘淇濇寚瀹氱殑鏈€灏忓閲?> 鏁扮粍缂撳啿鍖哄綋鍓嶇殑闀垮害
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    
 
    /**
     * 鏁扮粍缂撳啿鍖烘渶澶у瓨鍌ㄥ閲?     * - 涓€浜?VM 浼氬湪涓€涓暟缁勪腑瀛樺偍鏌愪簺鏁版嵁--->涓轰粈涔堣鍑忓幓 8 鐨勫師鍥?     * - 灏濊瘯鍒嗛厤杩欎釜鏈€澶у瓨鍌ㄥ閲忥紝鍙兘浼氬鑷?OutOfMemoryError(褰撹鍊?> VM 鐨勯檺鍒舵椂)
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 
    /**
     * 绉佹湁鏂规硶锛氭墿瀹癸紝浠ョ‘淇?ArrayList 鑷冲皯鑳藉瓨鍌?minCapacity 涓厓绱?     * - 鎵╁璁$畻锛歯ewCapacity = oldCapacity + (oldCapacity >> 1);  鎵╁厖褰撳墠瀹归噺鐨?.5鍊?     * @param minCapacity    鎸囧畾鐨勬渶灏忓閲?     */
    private void grow(int minCapacity) 
        // 闃叉婧㈠嚭浠g爜
        int oldCapacity = elementData.length;// 鏃у閲忓ぇ灏?        int newCapacity = oldCapacity + (oldCapacity >> 1); // 鏂板閲忎负鏃у閲忕殑1.5鍊?        if (newCapacity - minCapacity < 0)  // 鑻?newCapacity鏂板閲忓皬浜庡弬鏁版寚瀹氬閲忥紝淇敼鏂板閲?            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)   // 鑻?newCapacity 澶т簬鏈€澶у瓨鍌ㄥ閲忥紝鍒欒繘琛屽ぇ瀹归噺鍒嗛厤
            newCapacity = hugeCapacity(minCapacity);
        // 鎷疯礉鎵╁
        elementData = Arrays.copyOf(elementData, newCapacity);
    
    /**
     * 绉佹湁闈欐€佹柟娉曪細澶у閲忓垎閰嶏紝鏈€澶у垎閰?Integer.MAX_VALUE
     * @param minCapacity
     */
    private static int hugeCapacity(int minCapacity) 
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    

1锛夌‘淇濇暟缁勫凡浣跨敤闀垮害锛坰ize锛夊姞1涔嬪悗瓒冲瀛樹笅 涓嬩竴涓暟鎹?br /> 2锛変慨鏀规鏁癿odCount 鏍囪瘑鑷1锛屽鏋滃綋鍓嶆暟缁勫凡浣跨敤闀垮害锛坰ize锛夊姞1鍚庣殑澶т簬褰撳墠鐨勬暟缁勯暱搴︼紝鍒欒皟鐢╣row鏂规硶锛屽闀挎暟缁勶紝grow鏂规硶浼氬皢褰撳墠鏁扮粍鐨勯暱搴﹀彉涓哄師鏉ュ閲忕殑1.5鍊嶃€?br /> 3锛夌‘淇濇柊澧炵殑鏁版嵁鏈夊湴鏂瑰瓨鍌ㄤ箣鍚庯紝鍒欏皢鏂板厓绱犳坊鍔犲埌浣嶄簬size鐨勪綅缃笂銆?br /> 4锛夎繑鍥炴坊鍔犳垚鍔熷竷灏斿€笺€?/p>

remove()鏂规硶

鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱犮€?/p>

//鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱狅紝杩斿洖琚垹闄ょ殑鍏冪礌
public E remove(int index) 
    rangeCheck(index);

    modCount++; //琚垹闄ゆ椂  modCount涔熶細鏀瑰彉
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
         // 灏?index + 1 鍙婁箣鍚庣殑鍏冪礌鍚戝墠绉诲姩涓€浣嶏紝瑕嗙洊琚垹闄ゅ€?        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    //灏嗘渶鍚庝竴涓厓绱犵疆绌猴紝骞跺皢 size 鍊煎噺1   
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;

鐩存帴鍒犻櫎闆嗗悎涓煇涓厓绱狅紝濡傛灉鍏冪礌閲嶅锛屽垯鍙垹闄や笅鏍囨渶灏忕殑鍏冪礌

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;

蹇€熷垹闄ゆ柟娉?/p>

娌℃湁鍋氳竟鐣屾鏌ワ紝鐩存帴杩涜鍒犻櫎鎿嶄綔

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

鍒犻櫎鎿嶄綔鐨勬楠わ細

  1. 鑾峰彇鎸囧畾浣嶇疆 index 澶勭殑鍏冪礌鍊?/li>
  2. 灏?index + 1 鍙婁箣鍚庣殑鍏冪礌鍚戝墠绉诲姩涓€浣?/li>
  3. 灏嗘渶鍚庝竴涓厓绱犵疆绌猴紝骞跺皢 size 鍊煎噺 1
  4. 杩斿洖琚垹闄ゅ€硷紝瀹屾垚鍒犻櫎鎿嶄綔

涓轰粈涔圓rrayList缁ф壙浜咥bstractList杩樿瀹炵幇List鎺ュ彛 锛?/h3>

闃叉鍦ㄤ唬鐞嗘椂鍑洪敊

鍏跺疄娌℃湁蹇呰锛屽疄鐜颁笌涓嶅疄鐜伴兘鏄彲浠ョ殑銆?/p>

System.arraycopy()鍜孉rrays.copyof()

  1. Arrays.copyOf()鍐呴儴杩樿皟鐢ㄤ簡System.arraycopy()杩欎釜鏂规硶
  2. System.arraycopy()闇€瑕佷紶涓€涓暟缁勭殑鍙傛暟杩涜鎷疯礉銆?/li>
  3. 鑰孉rrays.copyof()鑷繁鍐呴儴浼氬垱寤轰竴涓柊鐨勬暟缁勮繘琛屾嫹璐?/li>

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

ArrayList婧愮爜鍒嗘瀽

Java 闆嗗悎

python瀛︿範绡?5 - 瀛楀吀 闆嗗悎

Scala闆嗗悎绫诲瀷璇﹁В

201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎

澶у巶鍓嶇寮€鍙戯紝蹇呭宸ュ叿闆嗗悎