Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細Vector

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細Vector相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%96%b0%e4%b8%80%e4%bb%a3' title='鏂颁竴浠?>鏂颁竴浠?/a>   nal   鎶ラ敊   ash   should   data   jdk   

Vector婧愮爜瀛︿範

鍓嶆枃浼犻€侀棬锛?br /> Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細LinkedList
Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細ArrayList
Vector鏄疛DK1.0涓殑闆嗗悎锛屾槸闆嗗悎涓殑鑰佸ぇ鍝ワ紝鍏朵腑澶ч儴鍒嗙殑鏂规硶閮借synchronized鍏抽敭瀛楁墍淇グ锛屼笌ArrayList鍜孡inkedList涓嶅悓锛屽畠鏄?strong>绾跨▼瀹夊叏鐨勶紙鍏充簬绾跨▼瀹夊叏锛屼箣鍚庡涔犲啀鍋氱郴缁熸€荤粨锛夈€備絾鏄殢鐫€涓€绯诲垪鐨勬洿鏂拌凯浠o紝瀹冪殑缂虹偣娓愭笎鏆撮湶锛氬鏂规硶鍚嶅瓧澶暱锛屽疄鐜版帴鍙f椂鍑虹幇浜嗚澶氶噸澶嶅浣欑殑鏂规硶绛夌瓑銆?br /> 浠嶫DK1.2寮€濮嬶紝Vector绫昏鏀硅繘浠ュ疄鐜癓ist鎺ュ彛锛岃瀹冩垚涓篔ava闆嗗悎妗嗘灦鐨勪竴鍛橈紝濡傛灉涓嶉渶瑕佺嚎绋嬪畨鍏紝寤鸿浣跨敤ArrayList锛屾晥鐜囨洿楂樸€?/p>

Vector缁ф壙浣撶郴

杩樻槸鎸夌収鎯緥锛屽厛鐪嬬湅瀹冪殑缁ф壙鍥撅紝褰撶劧杩欏紶鍥炬槸鍩轰簬JDK1.8鐨勩€?br /> 鎶€鏈浘鐗? src=

  • 鍙互鐪嬪嚭鏉ュ畠鍜?code>ArrayList涓€鏍凤紝缁ф壙浜?code>AbstractList锛屽疄鐜?code>List鎺ュ彛銆?/li>
  • 瀹炵幇浜?code>RandomAccess鎺ュ彛锛屾敮鎸侀殢鏈鸿闂€?/li>
  • 瀹炵幇浜?code>Cloneable鎺ュ彛锛屽疄鐜颁簡鍏嬮殕鐨勫姛鑳姐€?/li>
  • 瀹炵幇浜?code>Serializable鎺ュ彛锛屾敮鎸佸簭鍒楀寲銆?/li>

Vector鏍稿績婧愮爜

//瀛樺偍鍏冪礌鐨勬暟缁? protected Object[] elementData; //鍏冪礌涓暟 protected int elementCount; //璇ュ€煎喅瀹氫簡澧為暱鏈哄埗鐨勪笉鍚? protected int capacityIncrement; private static final long serialVersionUID = -2767605614048989439L;

鏋勯€犲櫒

鎴戜滑鍙互寰楀嚭缁撹锛?code>Vector鐨勫簳灞備篃鏄熀浜庢暟缁勫疄鐜扮殑锛屼絾鏄繖浜涘睘鎬у拰鎴戜滑涔嬪墠鎻愬埌鐨?code>ArrayList鏈変粈涔堜笉鍚屼箣澶勫憿锛熸垜浠户缁悜涓嬬湅瀹冩墍鎻愪緵鐨勫嚑涓瀯閫犲櫒锛?/p>

    //涓や釜鍙傛暟锛屽垱寤篿nitialCapacity澶у皬鐨勬暟缁勶紝骞朵负capacityIncrement璧嬪€?    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

    //甯︿竴涓弬鏁帮紝璋冪敤public Vector(int initialCapacity, int capacityIncrement)
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

    //鏃犲弬鏋勯€犲櫒锛岃皟鐢╒ector(int initialCapacity)
    public Vector() {
        this(10);
    }

    //浼犲叆闆嗗悎
    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        //绫诲瀷鍒ゆ柇
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

鎴戜滑鍙互鍙戠幇锛?/p>

  • initialCapacity浠h〃鐨勬槸鏁扮粍鐨勫閲忥紝鎴戜滑鍙互鎸囧畾瀹归噺锛?strong>涓嶆寚瀹氶粯璁や负10銆?/li>
  • capacityIncrement浠庡瓧闈笂鐪嬶紝灏卞彲浠ョ煡閬撳畠浠h〃鐨勬槸瀹归噺澧為噺锛屾剰鍛崇潃杩欎釜鍊煎皢浼氬奖鍝嶄箣鍚庣殑鎵╁锛屽彲浠ユ寚瀹氾紝涓嶆寚瀹氶粯璁や负0銆?/li>

鎵╁鏈哄埗

閭d箞鎴戜滑缁х画鏉ョ湅鐪嬪畠鐨?strong>鎵╁鏈哄埗锛屾槸鍚﹀彲浠ラ獙璇佹垜浠殑璇存硶锛?br /> 鍩烘湰涓婄殑閮ㄥ垎锛岄兘鏄拰ArrayList绫讳技锛屾垜浠洿鎺ユ埅鍙栨湁宸紓鐨勯儴鍒嗭細

    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        //濡傛灉澧為噺澶т簬0,鏂板閲?= 鍘熷閲?+ 澧為噺
        //濡傛灉澧為噺涓嶅ぇ浜?锛屾柊瀹归噺 = 鍘熷閲?2
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

鍏朵粬鐨勯儴鍒嗗氨涓嶅仛鍒嗘瀽浜嗭紝涔嬪墠璁茬殑寰堣缁嗭紝鍙互鐪嬬湅鍓嶉潰鐨勬枃绔犮€傛垜浠渶瑕佸叧娉ㄧ殑鏄細

  • 濡傛灉鎸囧畾澧為噺锛堝閲?gt;0)锛岄偅涔堟瘡娆″湪鎵╁鐨勬椂鍊欙紝鏂板閲忓氨鏄湪鍘熷閲忕殑鍩虹涓婂姞涓婃寚瀹氬閲?/strong>銆?/li>
  • 濡傛灉娌℃湁鎸囧畾澧為噺锛岄偅涔堟瘡娆″湪鎵╁鐨勬椂鍊欙紝鏂板閲忛粯璁ゅ彉鎴愬師瀹归噺鐨勪袱鍊?/strong>銆?/li>

Enumeration

姒傝堪

璇磋捣杩唬鍣紝鎴戜滑鎬绘槸绗竴涓兂鍒扮殑灏辨槸Iterator锛岃€屽啀Iterator鏄湪JDK1.2鐨勬椂鍊欒癁鐢熺殑锛岀敤浜庡彇浠DK1.0鐗堟湰鐨勫敮涓€杩唬鍣?code>Enumeration銆傚畼鏂瑰瀹冪殑瑙i噴鏄繖鏍风殑锛?/p>

An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.

鎴戠敤鎷欏姡鐨勮嫳璇瘯鐫€缈昏瘧涓€涓嬶細瀹炵幇Enumeration杩欎釜鎺ュ彛鐨勫璞″憿锛?strong>灏嗕細鐢熸垚涓€绯诲垪鐨勫厓绱狅紝鐢熸垚鐨勬椂鍊欐槸涓€涓竴涓敓鎴愮殑锛岄€氳繃璋冪敤nextElement杩欎釜鏂规硶锛屽氨鍙互杩斿洖杩欎釜绯诲垪閲屾墍鏈夌殑杩炵画鍏冪礌銆?/p>

浠婂ぉ鎴戜滑鍔卞織鍋氫釜鍏夎崳鐨勫畼鏂规枃妗f惉杩愬伐锛?/p>

Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream.

杩欐鐨勬剰鎬濅篃寰堟槑鐧斤細Enumeration鎺ュ彛涓?strong>Vector鐨勫厓绱?/strong>锛?strong>hashtable鐨勯敭鍜屽€?/strong>鎻愪緵浜嗘灇涓剧殑鏂规硶锛屽畠涔熻杩愮敤鍒版寚瀹?strong>SequenceInputStream鐨勮緭鍏ユ祦涓€傛垜浠殏鏃跺彧闇€瑕佺煡閬擄紝Vector绫讳腑锛屾湁涓€绉嶆柟娉曡兘澶熶骇鐢烢umeration瀵硅薄灏卞畬浜嬩簡銆傚叾浠栫殑鎴戜滑鍚庨潰浼氳繘琛屾€荤粨銆?/p>

鎺ヤ笅鏉ヨ繖娈佃瘽鐩稿綋鍏抽敭锛佸畼鏂规枃妗d腑鐢ㄤ簡澶у啓鐨凬OTE:

NOTE:The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.

澶ц嚧鐨勬剰鎬濆氨鏄細鐜板湪杩欎釜鏂规硶鍛紝涓嶅お閫傚簲娼祦浜嗭紝閭d釜骞翠唬鐢ㄨ捣鏉ユ尯涓嶉敊锛岀幇鍦ㄩ渶瑕佸勾杞讳竴杈堟潵浠f浛浜嗐€傝繖涓柊涓€浠g殑浜х墿灏辨槸Iterator锛屽畠澶嶅埗浜咵numeration鐨勫姛鑳?/strong>锛屽苟涓?strong>澧炲姞鍙€夌殑remove鎿嶄綔锛岃€屼笖鎻愪緵浜嗘洿绠€鐭殑鍛藉悕銆傚畼鏂逛豢浣涘湪瀣夌毊绗戣劯瀵逛綘璇达細浜诧紝杩欒竟寤鸿浣犺凯浠e櫒灏介噺鐢↖terator鍝熴€?/p>

浣嗘槸灏界濡傛锛屾垜浠繕闇€闇€瑕佷簡瑙d互涓嬪畠鐨勫熀鏈搷浣滐紝姣曠珶浠ュ悗鍙兘杩樻槸浼氳鍒般€?/p>

婧愮爜鎻忚堪

//Enumeration鎺ュ彛  
public interface Enumeration<E> {
    //鍒ゆ柇鏄惁杩樻湁鏇村鐨勫厓绱?    boolean hasMoreElements();
    //娌℃湁涓嬩竴涓厓绱犲氨鎶ラ敊锛屾湁灏辫繑鍥?    E nextElement();
}
//Vector涓殑elements鏂规硶瀵规帴鍙g殑瀹炵幇
public Enumeration<E> elements() {
    //閫氳繃鍖垮悕鍐呴儴绫诲疄鐜版帴鍙?    return new Enumeration<E>() {
        int count = 0;

        public boolean hasMoreElements() {
            return count < elementCount;
        }

        public E nextElement() {
            synchronized (Vector.this) {
                if (count < elementCount) {
                    return elementData(count++);
                }
            }
            throw new NoSuchElementException("Vector Enumeration");
        }
    };
}

鍏蜂綋鎿嶄綔

    Vector<String> v = new Vector<>();
    v.add("澶?quot;);
    v.add("涔?quot;);
    v.add("宸?quot;);
    v.add("澶?quot;);
    //鍒╃敤vector瀵硅薄浜х敓杩唬鍣ㄥ璞?    Enumeration<String> e = v.elements();
    //鍒ゆ柇鍚庤竟鏄惁杩樻湁鍏冪礌
    while(e.hasMoreElements()){
        //鎸姩鎸囬拡鎸囧悜涓嬩竴涓厓绱?        System.out.print(e.nextElement()+" ");
    }
    //澶?涔?宸?澶?

Vector鎬荤粨

  • 搴曞眰鍩轰簬鏁扮粍锛屽彲浠ュ疄鐜板姩鎬佹墿澧烇紝鏀寔鏍规嵁绱㈠紩蹇€熻闂€?/li>
  • 濡傛灉娌℃湁鎸囧畾瀹归噺锛岄粯璁や负10銆傚鏋滄病鏈夋寚瀹氬閲忥紝榛樿涓?銆?/li>
  • 鎵╁鏃讹紝濡傛灉澧為噺>0锛屽垯鏂板閲?鎸囧畾澧為噺+鍘熷閲?/strong>锛涘鏋滃閲?lt;=0锛屽垯鏂板閲忎负鍘熷閲忕殑涓ゅ€?/strong>銆傦紙娉ㄦ剰锛孷ector閲屼篃鏈夊疄鐜扮‘瀹氬閲忕殑鏂规硶ensureCapacity锛?/li>
  • 绾跨▼瀹夊叏锛屽鍦ㄥ垱寤鸿凯浠e櫒涔嬪悗鐨勪换浣曟椂闂村缁撴瀯杩涜淇敼锛岄櫎浜嗚凯浠e櫒鏈韩鐨剅emove鍜宎dd澶栵紝閮戒細鎶涘嚭ConcurrentModification寮傚父銆傛墍浠ワ紝Vector閫氳繃synchronized鍏抽敭瀛楀疄鐜扮嚎绋嬪悓姝ャ€傚彲浠ュ弬鑰冿細https://blog.csdn.net/yjclsx/article/details/85283169

鏈枃鑻ユ湁鍙欒堪涓嶅綋涔嬪锛岃繕鏈涘悇浣嶈瘎璁哄尯鍔犱互鎵硅瘎鎸囨锛岃阿璋€?/p>

鍙傝€冭祫鏂欙細JDK1.8瀹樻柟鏂囨。

以上是关于Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細Vector的主要内容,如果未能解决你的问题,请参考以下文章

涓簡婧愮爜鐨勬瘨锛岀粰浣犱竴鍓壇鑽?/a>

銆婂悙琛€鏁寸悊銆嬬郴鍒?鍓嶇寮€鍙戝伐鍏枫€佺粍浠跺簱闆嗗悎

銆婃繁搴﹀涔犱箣pytorch銆媝df+闄勪功婧愮爜

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

(c)2006-2024 SYSTEM All Rights Reserved IT常识