📜  Java.util.Vector类

📅  最后修改于: 2020-11-14 06:39:52             🧑  作者: Mango


介绍

java.util.Vector类实现可增长的对象数组。与数组类似,它包含可以使用整数索引访问的组件。以下是关于Vector的要点-

  • Vector的大小可以根据需要增加或缩小,以适应添加和删除项目的需要。

  • 每个向量通过保持capacitycapacityIncrement试图优化存储管理。

  • 从Java 2平台v1.2开始,对该类进行了改进以实现List接口。

  • 与新的集合实现不同, Vector是同步的。

  • 此类是Java Collections Framework的成员。

类声明

以下是java.util.Vector类的声明-

public class Vector
   extends AbstractList
   implements List, RandomAccess, Cloneable, Serializable

在这里表示一个Element,可以是任何类。例如,如果要构建一个整数数组列表,则可以按以下方式对其进行初始化-

ArrayList list = new ArrayList();  

类的构造函数

Sr.No. Constructor & Description
1

Vector()

This constructor is used to create an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.

2

Vector(Collection c)

This constructor is used to create a vector containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

3

Vector(int initialCapacity)

This constructor is used to create an empty vector with the specified initial capacity and with its capacity increment equal to zero.

4

Vector(int initialCapacity, int capacityIncrement)

This constructor is used to create an empty vector with the specified initial capacity and capacity increment.

类方法

Sr.No. Method & Description
1 boolean add(E e)

This method appends the specified element to the end of this Vector.

2 void add(int index, E element)

This method inserts the specified element at the specified position in this Vector.

3 boolean addAll(Collection c)

This method appends all of the elements in the specified Collection to the end of this Vector.

4 boolean addAll(int index, Collection c)

This method inserts all of the elements in the specified Collection into this Vector at the specified position.

5 void addElement(E obj)

This method adds the specified component to the end of this vector, increasing its size by one.

6 int capacity()

This method returns the current capacity of this vector.

7 void clear()

This method removes all of the elements from this vector.

8 clone clone()

This method returns a clone of this vector.

9 boolean contains(Object o)

This method returns true if this vector contains the specified element.

10 boolean containsAll(Collection c)

This method returns true if this Vector contains all of the elements in the specified Collection.

11 void copyInto(Object[ ] anArray)

This method copies the components of this vector into the specified array.

12 E elementAt(int index)

This method returns the component at the specified index.

13 Enumeration elements()

This method returns an enumeration of the components of this vector.

14 void ensureCapacity(int minCapacity)

This method increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.

15 boolean equals(Object o)

This method compares the specified Object with this Vector for equality.

16 E firstElement()

This method returns the first component (the item at index 0) of this vector.

17 E get(int index)

This method returns the element at the specified position in this Vector.

18 int hashCode()

This method returns the hash code value for this Vector.

19 int indexOf(Object o)

This method returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

20 int indexOf(Object o, int index)

This method returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.

21 void insertElementAt(E obj, int index)

This method inserts the specified object as a component in this vector at the specified index.

22 boolean isEmpty()

This method tests if this vector has no components.

23 E lastElement()

This method returns the last component of the vector.

24 int lastIndexOf(Object o)

This method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

25 int lastIndexOf(Object o, int index)

This method returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.

26 E remove(int index)

This method removes the element at the specified position in this Vector.

27 boolean remove(Object o)

This method removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.

28 boolean removeAll(Collection c)

This method removes from this Vector all of its elements that are contained in the specified Collection.

29 void removeAllElements()

This method removes all components from this vector and sets its size to zero.

30 boolean removeElement(Object obj)

This method removes the first occurrence of the argument from this vector.

31 void removeElementAt(int index)

This method deletes the component at the specified index.

32 protected void removeRange(int fromIndex, int toIndex)

This method removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.

33 boolean retainAll(Collection c)

This method retains only the elements in this Vector that are contained in the specified Collection.

34 E set(int index, E element)

This method replaces the element at the specified position in this Vector with the specified element.

35 void setElementAt(E obj, int index)

This method sets the component at the specified index of this vector to be the specified object.

36 void setSize(int newSize)

This method sets the size of this vector.

37 int size()

This method returns the number of components in this vector.

38 List subList(int fromIndex, int toIndex)

This method returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.

39 object[ ] toArray()

This method returns an array containing all of the elements in this Vector in the correct order.

40 T[ ] toArray(T[ ] a)

This method returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.

41 String toString()

This method returns a string representation of this Vector, containing the String representation of each element.

42 void trimToSize()

This method trims the capacity of this vector to be the vector’s current size.

方法继承

此类从以下类继承方法-

  • java.util.AbstractMap
  • java.lang.Object
  • java.util.List