国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Vector

2019-11-11 02:49:11
字體:
供稿:網(wǎng)友

  在java提高篇(二一)—–ArrayList、java提高篇(二二)—LinkedList,詳細講解了ArrayList、linkedList的原理和實現(xiàn)過程,對于List接口這里還介紹一個它的實現(xiàn)類Vector,Vector 類可以實現(xiàn)可增長的對象數(shù)組。

一、Vector簡介

        Vector可以實現(xiàn)可增長的對象數(shù)組。與數(shù)組一樣,它包含可以使用整數(shù)索引進行訪問的組件。不過,Vector的大小是可以增加或者減小的,以便適應創(chuàng)建Vector后進行添加或者刪除操作。

        Vector實現(xiàn)List接口,繼承AbstractList類,所以我們可以將其看做隊列,支持相關(guān)的添加、刪除、修改、遍歷等功能。

        Vector實現(xiàn)Randmoaccess接口,即提供了隨機訪問功能,提供提供快速訪問功能。在Vector我們可以直接訪問元素。

        Vector 實現(xiàn)了Cloneable接口,支持clone()方法,可以被克隆。

[java] view plain copy 在CODE上查看代碼片public class Vector<E>      extends AbstractList<E>      implements List<E>, RandomAccess, Cloneable, java.io.Serializable  

        Vector提供了四個構(gòu)造函數(shù):

[java] view%20plain copy /**      * 構(gòu)造一個空向量,使其內(nèi)部數(shù)據(jù)數(shù)組的大小為 10,其標準容量增量為零。      */       public Vector() {              this(10);       }            /**      * 構(gòu)造一個包含指定 collection 中的元素的向量,這些元素按其 collection 的迭代器返回元素的順序排列。      */      public Vector(Collection<? extends E> c) {          elementData = c.toArray();          elementCount = elementData.length;          // c.toArray might (incorrectly) not return Object[] (see 6260652)          if (elementData.getClass() != Object[].class)              elementData = Arrays.copyOf(elementData, elementCount,                      Object[].class);      }            /**      * 使用指定的初始容量和等于零的容量增量構(gòu)造一個空向量。      */      public Vector(int initialCapacity) {          this(initialCapacity, 0);      }            /**      *  使用指定的初始容量和容量增量構(gòu)造一個空的向量。      */      public Vector(int initialCapacity, int capacityIncrement) {          super();          if (initialCapacity < 0)              throw new IllegalArgumentException("Illegal Capacity: "+                                                 initialCapacity);          this.elementData = new Object[initialCapacity];          this.capacityIncrement = capacityIncrement;      }  

        在成員變量方面,Vector提供了elementData%20,%20elementCount,%20capacityIncrement三個成員變量。其中

        elementData%20:"Object[]類型的數(shù)組",它保存了Vector中的元素。按照Vector的設(shè)計elementData為一個動態(tài)數(shù)組,可以隨著元素的增加而動態(tài)的增長,其具體的增加方式后面提到(ensureCapacity方法)。如果在初始化Vector時沒有指定容器大小,則使用默認大小為10.

        elementCount:Vector 對象中的有效組件數(shù)。

        capacityIncrement:向量的大小大于其容量時,容量自動增加的量。如果在創(chuàng)建Vector時,指定了capacityIncrement的大小;則,每次當Vector中動態(tài)數(shù)組容量增加時>,增加的大小都是capacityIncrement。如果容量的增量小于等于零,則每次需要增大容量時,向量的容量將增大一倍。

        同時Vector是線程安全的!

二、public synchronized boolean add(E e) {          modCount++;               ensureCapacityHelper(elementCount + 1);    //確認容器大小,如果操作容量則擴容操作          elementData[elementCount++] = e;   //將e元素添加至末尾          return true;      }  

        這個方法相對而言比較簡單,具體過程就是先確認容器的大小,看是否需要進行擴容操作,然后將E元素添加到此向量的末尾。

[java] view%20plain copy /**      * 從Vector容器中移除指定元素E      */      public boolean remove(Object o) {          return removeElement(o);      }        public synchronized boolean removeElement(Object obj) {          modCount++;          int i = indexOf(obj);   //計算obj在Vector容器中位置          if (i >= 0) {              removeElementAt(i);   //移除              return true;          }          return false;      }            public synchronized void removeElementAt(int index) {          modCount++;     //修改次數(shù)+1          if (index >= elementCount) {   //刪除位置大于容器有效大小              throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);          }          else if (index < 0) {    //位置小于 < 0              throw new ArrayIndexOutOfBoundsException(index);          }          int j = elementCount - index - 1;          if (j > 0) {                 //從指定源數(shù)組中復制一個數(shù)組,復制從指定的位置開始,到目標數(shù)組的指定位置結(jié)束。              //也就是數(shù)組元素從j位置往前移              System.arraycopy(elementData, index + 1, elementData, index, j);          }          elementCount--;   //容器中有效組件個數(shù) - 1          elementData[elementCount] = null;    //將向量的末尾位置設(shè)置為null      }  

        因為Vector底層是使用數(shù)組實現(xiàn)的,所以它的操作都是對數(shù)組進行操作,只不過其是可以隨著元素的增加而動態(tài)的改變?nèi)萘看笮。鋵崿F(xiàn)方法是是使用Arrays.copyOf方法將舊數(shù)據(jù)拷貝到一個新的大容量數(shù)組中。Vector的整個內(nèi)部實現(xiàn)都比較簡單,這里就不在重述了。

三、Vector遍歷        Vector支持4種遍歷方式。

3.1、隨機訪問        因為Vector實現(xiàn)了RandmoAccess接口,可以通過下標來進行隨機訪問。

[java] view%20plain copy for(int i = 0 ; i < vec.size() ; i++){          value = vec.get(i);      }  

3.2、迭代器

[java] view%20plain copy Iterator it = vec.iterator();      while(it.hasNext()){          value = it.next();          //do something      }  

3.2、for循環(huán)

[java] view%20plain copy for(Integer value:vec){          //do something      }  

3.4、Enumeration循環(huán)

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片Vector vec = new Vector<>();      Enumeration enu = vec.elements();      while (enu.hasMoreElements()) {          value = (Integer)enu.nextElement();      }  
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 松滋市| 汾西县| 吴旗县| 和顺县| 柘城县| 永寿县| 安岳县| 新巴尔虎左旗| 手游| 德昌县| 白朗县| 晋宁县| 石嘴山市| 咸丰县| 金寨县| 乌兰浩特市| 镇雄县| 犍为县| 大名县| 马山县| 柳州市| 敦化市| 墨江| 微山县| 揭西县| 大化| 房山区| 兰州市| 南木林县| 定南县| 齐齐哈尔市| 宜宾市| 十堰市| 辽阳县| 施秉县| 五家渠市| 兰溪市| 张家港市| 杂多县| 安塞县| 嘉黎县|