Vector源碼學(xué)習(xí)

/** * The {@code Vector} class implements a growable array of * objects. Like an array, it contains components that can be * accessed using an integer index. However, the size of a * {@code Vector} can grow or shrink as needed to accommodate * adding and removing items after the {@code Vector} has been created. * *

Vector實現(xiàn)了一個可增長的數(shù)組集合。如同數(shù)組,他可以使用整形索引index來訪問元素。然而,vector在創(chuàng)建實例后,可以通過增加和刪除元素來增長或減少vector的大小。

Each vector tries to optimize storage management by maintaining a

* {@code capacity} and a {@code capacityIncrement}. The

* {@code capacity} is always at least as large as the vector

* size; it is usually larger because as components are added to the

* vector, the vector's storage increases in chunks the size of

* {@code capacityIncrement}. An application can increase the

* capacity of a vector before inserting a large number of

* components; this reduces the amount of incremental reallocation.

*

*

* The iterators returned by this class's {@link #iterator() iterator} and * {@link #listIterator(int) listIterator} methods arefail-fast: * if the vector is structurally modified at any time after the iterator is * created, in any way except through the iterator's own * {@link ListIterator#remove() remove} or * {@link ListIterator#add(Object) add} methods, the iterator will throw a * {@link ConcurrentModificationException}.? Thus, in the face of * concurrent modification, the iterator fails quickly and cleanly, rather * than risking arbitrary, non-deterministic behavior at an undetermined * time in the future.? The {@link Enumeration Enumerations} returned by * the {@link #elements() elements} method arenotfail-fast. * *

Note that the fail-fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification.? Fail-fast iterators * throw {@code ConcurrentModificationException} on a best-effort basis. * Therefore, it would be wrong to write a program that depended on this * exception for its correctness:the fail-fast behavior of iterators

* should be used only to detect bugs.* *

As of the Java 2 platform v1.2, this class was retrofitted to

* implement the {@link List} interface, making it a member of the

* * Java Collections Framework.? Unlike the new collection * implementations, {@code Vector} is synchronized.? If a thread-safe * implementation is not needed, it is recommended to use {@link * ArrayList} in place of {@code Vector}.

實現(xiàn)了list接口,稱為collection框架的一員,與新的collection不同的是,vector是線程安全的。如果不需要線程安全,建議使用arrayList代替Vector.

多種初始化方式

Vector():默認容量10,增長0

Vector(size):容量size,增長0

Vector(size,increaseMount):容量size,增長increaseMount

Vector(new Collection()):使用實現(xiàn)了Collection接口類的實例來初始化。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容