在Java2之前,Java是没有完整的集合框架的。当时只有一些简单的可以自扩展的容器类,比如Vector,Stack,Hashtable等。
java.util.Vector中包含的元素可以通过一个整型的索引值取得,它的大小可以在添加或移除元素时自动增加或缩小。Vector的操作很简单,通过addElement()加入一个对象,用elementAt()取出它,还可以查询当前所保存的对象的个数size();
用nextElement()获得下一个元素。Enumeration的用意在于使你能完全不用理会你要遍历的容器的基础结构,只关注你的遍历方法,
这也就使得遍历方法的重用成为可能。由于这种思想的强大功能,所以在Java2中被保留下来,不过具体实现,方法名和内部算法都改变了,这就是Java2中的Iterator以及ListIterator类。然而Enumeration的功能却十分有限,比如只能朝一个方向进行,只能读取而不能更改等。
<wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">:同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步的.<br style="line-height:25px"> 在Vector文档中可以看到它的很多函数都加了synchronized,从中可知道它的线程安全是通过Vector对象锁来完成。<br style="line-height:25px"> 即每个线程调用这些函数必须获得Vector对象锁,才能继续函数的执行,否则该线程只有等待直到得到该锁。<br style="line-height:25px"> 当然效率就很低</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">数据增长:其大小也是动态增长的。当需要增长时,Vector默认增长为原来一培,而ArrayList却是原来的一半</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">里面的元素可以为null</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:<span style="color:#993300; line-height:25px">asynchronizedboolean</span><span style="color:#000080; line-height:25px"></span><span style="color:#ff6600; line-height:25px">add</span><span style="color:#000080; line-height:25px">(Eobject)和</span><span style="color:#993300; line-height:25px">synchronizedvoid</span><span style="color:#000080; line-height:25px"></span><span style="color:#ff6600; line-height:25px">addElement</span><span style="color:#000080; line-height:25px">(Eobject)功能都是一样的。</span><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">addElement</span>是java1.2前的,是历史遗留。<span style="color:#ff6600; line-height:25px">add</span>是Java1.2新加的。<span style="color:#ff9900; line-height:25px">remove</span>方法同理。</wbr></wbr></wbr></wbr>
构造方法
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Vector.html#Vector()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Vector</a></span>()</nobr>
Constructs a new vector using the default capacity.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Vector.html#Vector(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Vector</a></span>(int capacity)</nobr>
Constructs a new vector using the specified capacity.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Vector.html#Vector(int,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Vector</a></span>(int capacity, int capacityIncrement)</nobr>
Constructs a new vector using the specified capacity and capacity increment.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Vector.html#Vector(java.util.Collection<?%20extends%20E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Vector</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Collection</a><?extendsE> collection)</nobr>
Constructs a new instance of
Vector containing the elements in
collection .
|