Java 中的 Vector 类函数

首页 / Java入门教程 / Java 中的 Vector 类函数

Vector实现了动态数组。它类似于ArrayList,线程同步sychronized。

以下是vector类提供的构造函数的列表。

Sr.No.Constructor & Remark
1

Vector()

此构造函数创建一个默认向量,其初始大小为10。

2

Vector(int size)

此构造函数接受等于所需大小的参数,并创建一个向量,其初始容量由大小指定。

3

Vector(int size,int incr)

此构造函数创建一个向量,其初始容量由大小指定,其增量由incr指定。

4

Vector(Collection c)

此构造函数创建一个包含集合c的元素的向量。

除了从其父类继承的方法外,Vector还定义了以下方法-

Sr.No.Method & Remark
1

void add(int index,Object element)

将指定的元素插入此Vector中的指定位置。

2

boolean add(Object o)

将指定的元素附加到此Vector的末尾。

3

boolean addAll(Collection c)

按照指定Collection的Iterator返回的顺序,将指定Collection中的所有元素附加到此Vector的末尾。

4

boolean addAll(int index,Collection c)

将指定Collection中的所有元素插入此Vector中的指定位置。

5

void addElement(Object obj)

将指定的分量添加到此向量的末尾,将其大小增加一。

6

int Capacity()

返回此向量的当前容量。

7

void clear()

从此向量中删除所有元素。

8

Object clone()

返回此向量的副本。

9

boolean contains(Object elem)

判断是否包含elem对象

10

boolean containsAll(Collection c)

判断是否包含指定Collection中的所有元素,则返回true。

11

void copyInto(Object [] anArray)

将此向量的元素复制到指定的数组中。

链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-vector-class.html

来源:LearnFk无涯教程网

12

Object elementAt(int index)

无涯教程网

返回指定索引处的组件。

13

Enumeration elements()

返回此向量的组成部分的枚举。

14

void sureCapacity(int minCapacity)

如有必要,增加此向量的容量,以确保它至少可以容纳最小容量参数指定的组件数。

15

boolean equals(Object o)

比较指定对象是否相等。

16

Object firstElement()

返回第一个元素(索引为0的项目)。

17

Object get(int index)

返回指定位置的元素。

18

int hashCode()

返回hashCode值。

19

int indexOf(Object elem)

搜索给定参数的第一个匹配项,使用equals方法判断是否相等。

20

int indexOf(Object elem.int index)

搜索给定参数的第一次出现,从索引处开始搜索,并使用equals方法判断是否相等。

21

void insertElementAt(Object obj,int index)

将指定对象作为组件插入指定索引处。

22

boolean isEmpty()

判断是否为空。

23

Object lastElement()

返回最后一个元素。

24

int lastIndexOf(Object elem)

返回指定对象最后一次出现的索引。

25

int lastIndexOf(Object elem,int index)

从指定的索引开始向后搜索指定的对象,并向其返回索引。

26

Object remove(int index)

移除指定位置的元素。

27

boolean remove(Object o)

移除第一次出现的对象,如果向量不包含该对象,则该对象不变。

28

boolean removeAll(Collection c)

移除所有包含在指定Collection中的元素。

29

void removeAllElements()

删除所有元素,并将其大小设置为零。

30

boolean removeElement(Object obj)

删除参数的第一个对象。

31

void removeElementAt(int index)

removeElementAt(int index)。

32

protected void removeRange(int fromIndex,int toIndex)

从此列表中删除索引在fromIndex(包括)和toIndex(不包括)之间的所有元素。

33

boolean keepAll(Collection c)

仅保留包含在指定Collection中的元素。

34

Object set(int index,Object element)

用指定元素替换指定位置的元素。

35

void setElementAt(Object obj,int index)

将指定索引处的组件设置为指定对象。

36

void setSize(int newSize)

设置大小。

37

int size()

返回大小数。

38

List subList(int fromIndex,int toIndex)

返回此列表中fromIndex(包括)和toIndex(不包括)之间的元素。

39

Object [] toArray()

返回所有元素的数组。

40

Object [] toArray(Object [] a)

返回所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。

41

String toString()

返回此向量的字符串表示形式,其中包含每个元素的String表示形式。

42

void trimToSize()

将该向量的容量修整为向量的当前大小。

Vector 示例

以下程序说明了此集合支持的几种方法-

import java.util.*;
public class VectorDemo {

   public static void main(String args[]) {
      //初始大小为 3,增量为 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.");
         
      //枚举向量中的元素。
      Enumeration vEnum=v.elements();
      System.out.println("\nElements in vector:");
      
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

这将产生以下输出-

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

硅谷产品实战36讲 -〔曲晓音〕

玩转Spring全家桶 -〔丁雪丰〕

OpenResty从入门到实战 -〔温铭〕

玩转Vue 3全家桶 -〔大圣〕

网络排查案例课 -〔杨胜辉〕

深入浅出可观测性 -〔翁一磊〕

后端工程师的高阶面经 -〔邓明〕

深入拆解消息队列47讲 -〔许文强〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)