Java 简明教程

Java Vector Class

Introduction

Vector 实现了一个动态数组。它类似于 ArrayList,但有以下两个区别:

Vector implements a dynamic array. It is similar to ArrayList, but with two differences −

  1. Vector is synchronized.

  2. Vector contains many legacy methods that are not part of the collections framework.

如果你事先不知道数组的大小,或者只需要一个可以在程序生命周期内变大小的数组,Vector 会非常有用。

Vector proves to be very useful if you don’t know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.

java.util.Vector 类实现了可增长的对象数组。类似于 Array,它包含可以使用整数索引访问的组件。以下是有关 Vector 的重要事项:

The java.util.Vector class implements a growable array of objects. Similar to an Array, it contains components that can be accessed using an integer index. Following are the important points about Vector −

  1. The size of a Vector can grow or shrink as needed to accommodate adding and removing items.

  2. Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement.

  3. As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface.

  4. Unlike the new collection implementations, Vector is synchronized.

  5. This class is a member of the Java Collections Framework.

Class declaration

下面是 java.util.Vector 的声明 −

Following is the declaration for java.util.Vector class −

public class Vector<E>
   extends AbstractList<E>
   implements List<E>, RandomAccess, Cloneable, Serializable

此处 <E> 代表一个元素,该元素可以是任何类。例如,如果你正在构建一个整数数组列表,则你可以按照如下方式对其进行初始化 −

Here <E> represents an Element, which could be any class. For example, if you’re building an array list of Integers then you’d initialize it as follows −

ArrayList<Integer> list = new ArrayList<Integer>();

Class constructors

Class methods

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.util.AbstractMap

  2. java.lang.Object

  3. java.util.List

Adding Elements and Iterating a Vector Example

下面的程序说明了 Vector 集合支持的几种方法:

The following program illustrates several of the methods supported by Vector collection −

import java.util.*;
public class VectorDemo {

   public static void main(String args[]) {
      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);
      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " + v.capacity());

      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));
      System.out.println("Capacity after four additions: " + v.capacity());

      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " + v.capacity());

      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));
      System.out.println("Current capacity: " + v.capacity());

      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));
      System.out.println("Current capacity: " + v.capacity());

      v.addElement(new Integer(11));
      v.addElement(new Integer(12));
      System.out.println("First element: " + (Integer)v.firstElement());
      System.out.println("Last element: " + (Integer)v.lastElement());

      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");

      // enumerate the elements in the vector.
      Enumeration vEnum = v.elements();
      System.out.println("\nElements in vector:");

      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

这会产生以下结果 −

This will produce the following result −

Output

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12