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 />
- 鍙互鐪嬪嚭鏉ュ畠鍜?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 - 绾跨▼瀹夊叏锛屽鍦ㄥ垱寤鸿凯浠e櫒涔嬪悗鐨勪换浣曟椂闂村缁撴瀯杩涜淇敼锛岄櫎浜嗚凯浠e櫒鏈韩鐨剅emove鍜宎dd澶栵紝閮戒細鎶涘嚭ConcurrentModification寮傚父銆傛墍浠ワ紝Vector閫氳繃
鏈枃鑻ユ湁鍙欒堪涓嶅綋涔嬪锛岃繕鏈涘悇浣嶈瘎璁哄尯鍔犱互鎵硅瘎鎸囨锛岃阿璋€?/p>
鍙傝€冭祫鏂欙細JDK1.8瀹樻柟鏂囨。
以上是关于Java灏忕櫧闆嗗悎婧愮爜鐨勫涔犵郴鍒楋細Vector的主要内容,如果未能解决你的问题,请参考以下文章