Vector

在Java2之前,Java是没有完整的集合框架的。当时只有一些简单的可以自扩展的容器类,比如Vector,Stack,Hashtable等。

java.util.Vector中包含的元素可以通过一个整型的索引值取得,它的大小可以在添加或移除元素时自动增加或缩小。Vector的操作很简单,通过addElement()加入一个对象,用elementAt()取出它,还可以查询当前所保存的对象的个数size();

另外还有一个Enumeration类提供了连续操作Vector中元素的方法,这可以通过Vector中的elements()方法来获取一个Enumeration类的对象,可以用一个While循环来遍历其中的元素。用hasMoreElements()检查其中是否还有更多的元素。
用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&lt;?%20extends%20E&gt;)" 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>&lt;?extendsE&gt; collection)</nobr>
Constructs a new instance of Vectorcontaining the elements in collection.
<wbr style="line-height:25px">例1</wbr><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Vector&lt;Integer&gt;v=newVector();<br style="line-height:25px"> v.add(newInteger(1));<br style="line-height:25px"> v.add(newInteger(2));<br style="line-height:25px"> Enumeration&lt;Integer&gt;e=v.elements();<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(e.hasMoreElements())<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println(e.nextElement());<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">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">v.add("1");<br style="line-height:25px"> v.add("2");<br style="line-height:25px"> v.add("3");<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(!v.isEmpty())<br style="line-height:25px"> System.out.println(v.remove(0));</span></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值