Java Collections Framework之Vector
openkk
13年前
<div class="BlogContent TextContent"> <p>Hierarchy如下:</p> <p><img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/42a2956129235d615bc664cf385a1b2e.jpg" width="320" height="331" /></p> <p>该类的主要方法如下图所示:</p> <p><img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/de841853b8af46769781f65223d258f4.png" width="304" height="894" /></p> <p>三个protect字段</p> <p> capacityIncrement:每次数组扩充的个数,如果为0,新容量是原来的2倍, 如果不为0,新容量为原来的容量加上此长度,代码体现在:</p> <p> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/a2cb85c0276f0737e3cf65b98c47b2d6.jpg" width="481" height="37" /></p> <p> elementCount : 数组中元素的个数。<br /> elementData:为该Vector的实现数组。</p> <p> 含有两个参数的构造函数:</p> <p> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/fdaac561a4ffa63a3c22493aa535e139.jpg" width="583" height="164" /><br /> initialCapacity代表该数组的初始化长度,capacityIncrement代表该数组满后的扩张因子。</p> <p> 含有一个参数的构造函数:<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/21774f02d5f052177c8e07a7ce645b29.jpg" width="375" height="68" /><br /> 此时capacityIncrement为0</p> <p> 不含参数的构造函数:<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/2895015aed62f76c16e2492f4d71e51b.jpg" width="288" height="64" /><br /> 此时的initialCapacity为10 capacityIncrement为0</p> <p> 构造参数类型为Collection的构造函数<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/a4788632b8fcf65cb1eedeb1b2cfa8de.jpg" width="653" height="132" /></p> <p> add(E e):向该Vector中添加一元素<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/c21d6775e9222bf8d6cafd218dab8abe.jpg" width="384" height="103" /><br /> 此方法是线程安全的,在给数组新的index赋值前会调用ensureCapacityHelper方法,保证改数组的容量在占满时扩充为新的数组。<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/5058dfd588c76d97911f6ed50fce18e3.jpg" width="651" height="231" /><br /> 扩充为新数组的调用的是<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/21774f02d5f052177c8e07a7ce645b29.jpg" width="544" height="67" /><br /> 此方法为native方法,依赖于具体的JVM实现。</p> <p> add(int index, E element):在指定的位置插入元素<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/12ff11ad25bb40f8c53b5ab50b643b41.jpg" width="416" height="58" /><br /> </p> <p><img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/52e3d97798c7d673d64becf7a48b98e0.jpg" width="719" height="196" /></p> <p> 此时 该index位置的元素向后移动,然后把obj插入index,这样便影响了效率,该方法同样要检查内部数组是否需要扩张。</p> <p> addAll(int index, Collection <!--? extends E--> c):<br /> 将原来index处元素向右移动c.toArray().length个长度,然后将c中的元素复制到elementData数组中。</p> <p> addAll(Collection <!--? extends E--> c):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/9b7ce027515bfee26032e39fa46d8e73.jpg" width="623" height="158" /><br /> 将c中的元素依次复制到elementData数组中(线程安全)</p> <p> <span style="color:#e53333;">addElement(E obj):</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/fc1cd66ea0515f7694f615dfce075d14.jpg" width="435" height="94" /><br /> 添加一个元素。</p> <p> <span style="color:#e56600;"> </span><span style="color:#e56600;">capacity():</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/28e97c1003530e9a42c5cb49c0105fbf.jpg" width="329" height="59" /><br /> 返回该内部数组的长度。</p> <p> clear():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/af23fb02a8a254741cec558d56c39f4f.jpg" width="238" height="49" /><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/f18995a78f24b62e37e827db03736f3b.jpg" width="423" height="139" /><br /> 清空该Vector。</p> <p> <span style="color:#e53333;">clone():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/18bfa19c0effed548f00e5402119ca86.jpg" width="507" height="188" /><br /> </span><span style="color:#333333;">通过该Vector克隆一个新的Vector,<span style="color:#e53333;">该克隆为浅拷贝</span>。<br /> </span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">contains(Object o):</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/dac3caf03f6b853a07cdc40a5a3f29f7.jpg" width="350" height="55" /><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/6b0efca227fecbdad60e87ed23950134.jpg" width="485" height="208" /><br /> 判断该Vector是否含有某对象o</span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">containsAll(Collection <!--?--> c):</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/19f511b4b9075d639ddfd7aea04cb0ef.jpg" width="499" height="63" /><br /> 调用父类AbstractCollection <e> 的containsAll(Collection <!--?--> c)的方法。 </e></span></p> <p><span style="color:#333333;"> copyInto(Object[] anArray):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/9371f3587cb9474cd75428899901e30d.jpg" width="519" height="52" /><br /> 将该Vector中的元素拷贝至anArray中。</span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">elementAt(int index):</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/979887b30fc8cf510524fecb99f5a26f.jpg" width="642" height="126" /><br /> 获取索引为index位置的元素。</span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">elements():</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/d144e4c1478243536907ca989ce3ac9b.jpg" width="529" height="293" /><br /> 返回该Vector的枚举表示。</span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">firstElement():</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/c13cfae6f7c3620e695952dc91bb0e9f.jpg" width="433" height="111" /><br /> 返回该集合的第一个元素。</span></p> <p><span style="color:#333333;"> <span style="color:#e53333;">get(int index):</span><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/7f969b59bdf943ab2fd49067092a3ff3.jpg" width="475" height="115" /><br /> 根据索引找到指定的元素。</span></p> <p><span style="color:#333333;"> insertElementAt(E obj, int index):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/7ee8253c90b8f910dd8f4366b2e11e2c.jpg" width="703" height="188" /><br /> 在指定的位置插入某元素。</span></p> <p><span style="color:#333333;"> isEmpty():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/c64befdca964e82af9454291dd09ab54.jpg" width="348" height="54" /><br /> 判断该集合是否为空。</span></p> <p><span style="color:#333333;"> lastElement():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/7e5ce280264d70fbdbe1de9febca2759.jpg" width="374" height="119" /> <br /> 该集合的最后一个元素。</span></p> <p><span style="color:#333333;"> lastIndexOf(Object o, int index):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/7a1c70294556662b3f0fee126af65e41.jpg" width="585" height="256" /><br /> 从该集合的index位置向前查找第一次出现该o的索引。</span></p> <p><span style="color:#333333;"> remove(int index):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/fc0d99dfb301dbd28f0a298508eb2bc3.jpg" width="506" height="256" /><br /> 删除该index处的元素,同时后面的元素向前移动,这样会消耗一定的时间。</span></p> <p><span style="color:#333333;"> remove(Object o):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/9e919f3951296fea75ebf5241cf01509.jpg" width="312" height="62" /><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/77891223c73d94c8e50f7792bdfe7b2e.jpg" width="494" height="162" /><br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/1f4e2f5d1f36891b49966ec02609929e.jpg" width="553" height="298" /><br /> 从该集合中删除特定的o,但是只会删除第一次出现的这个o,同时后面的依次向左移动。</span></p> <p><span style="color:#333333;"> removeAll(Collection <!--?--> c):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/2c3e0b15ffd9c46f5461a0a034903a99.jpg" width="483" height="80" /><br /> 从该集合中删除c中含有的元素。</span></p> <p><span style="color:#333333;"> removeRange(int fromIndex, int toIndex):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/d2020155610551e7312a4af97e468116.jpg" width="589" height="201" /><br /> 移除fromIndex到toIndex之间的元素,同时后面的元素左移,空的set为null以便GC回收。</span></p> <p><span style="color:#333333;"> retainAll(Collection <!--?--> c):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/3289c6b2e563ad7f8242765a7c04d0ba.jpg" width="476" height="57" /><br /> 删除该集合中含有c集合中的元素。</span></p> <p><span style="color:#333333;"> set(int index, E element):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/daed923ee043dd64519ee785c8d7e03f.jpg" width="464" height="136" /><br /> 将指定的index位置设置为element,不涉及到前后元素的移动。</span></p> <p><span style="color:#333333;"> setSize(int newSize):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/36d07e700d725160bd62749217dbbef7.jpg" width="462" height="198" /><br /> 改变该集合的容量。</span></p> <p><span style="color:#333333;"> size():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/1b23cbdcac7e8394684e7c4492ff56f3.jpg" width="324" height="54" /><br /> 返回该集合的大小。</span></p> <p><span style="color:#333333;"> subList(int fromIndex, int toIndex):<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/455fa54606d9b0b627d4d0ba493fdb5d.jpg" width="628" height="76" /><br /> 返回该集合的子集合。</span></p> <p><span style="color:#333333;"> toArray() :<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/ed0b6686499b3a30a61a62375e4e7ccf.jpg" width="443" height="72" /><br /> 返回该集合的Object数组表示。</span></p> <p><span style="color:#333333;"> toArray(T[] a):<br /> 返回该集合的特定的数组表示。</span></p> <p><span style="color:#333333;"> trimToSize():<br /> <img alt="Java Collections Framework之Vector" src="https://simg.open-open.com/show/c213ac0f44a277ff01a98bf737b3f87b.jpg" width="523" height="127" /><br /> 缩小容量,恰好撑得下该集合中的元素。</span></p> <p><span style="color:#333333;"> 总结:<br /> 该集合中的所有方法都是线程安全的,保证了状态一致性但是牺牲了运行效率。<br /> 根据索引获取元素很快,按索引删除元素要涉及到后续元素的向前移动,根据对象删除的话还需要先查到该<br /> 元素在集合中的索引,查找某一元素速度相对快些。<span style="color:#e53333;">插入快,删除相对慢,查找相对快。</span></span></p> </div>