摘自百度百科:

1. Vector 类在 java 中可以实现自动增长的对象数组;

创建了一个向量类的对象后,可以往其中随意地插入不同的类的对象,既不需顾及类型也不需预先选定向量的容量,并可方便地进行查找。对于预先不知或不愿预先定义数组大小,并需频繁进行查找、插入和删除工作的情况,可以考虑使用向量类。向量类提供了三种构造方法:
public Vector()
public Vector(int initialcapacity,int capacityIncrement)
public Vector(int initialcapacity)
使用第一种方法,系统会自动对向量对象进行管理。若使用后两种方法,则系统将根据参数initialcapacity设定向量对象的容量(即向量对象可存储数据的大小),当真正存放的数据个数超过容量时,系统会扩充向量对象的存储容量。
参数capacityIncrement给定了每次扩充的扩充值。当capacityIncrement为0时,则每次扩充一倍。利用这个功能可以优化存储。
2. Java中,数组对象一旦创建后,其元素的个数 不能被修改。而Java.util包中的Vector类(向量)提供类似于数组的能力,且能够动态地调整自身的大小。Vector类似于一个数组,但与数组相比在使用上有两个优点:
① 使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加;
② Vector类提供额外的方法来增加、删除元素,比数组操作高效。[1]

插入功能

(1)public final synchronized void addElement(Object obj)
将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象,可在其中插入不同类的对象。但插入的应是对象而不是数值,所以插入数值时要注意将数值转换成相应的对象。
例 要插入一个整数1时,不要直接调用v1.addElement(1),正确的方法为:
1
2
3
Vectorv1=new Vector();
Integerinteger1=new Integer(1);
v1.addElement(integer1);

(2)public final synchronized void setElementAt(object obj,int index)
将index处的对象设成obj,原来的对象将被覆盖。
(3)public final synchronized void insertElementAt(Object obj,int index)
在index指定的位置插入obj,原来对象以及此后的对象依次往后顺延。

删除功能

(1)public final synchronized void removeElement(Object obj)
从向量中删除obj。若有多个存在,则从向量头开始试,删除找到的第一个与obj相同的向量成员。
(2)public final synchronized void removeAllElement()
删除向量中所有的对象。
(3)public final synchronized void removeElementlAt(int index)
删除index所指的地方的对象。

查询搜索功能

(1)public final int indexOf(Object obj)
从向量头开始搜索obj,返回所遇到的第一个obj对应的下标,若不存在此obj,返回-1。
(2)public final synchronized int indexOf(Object obj,int index)
从index所表示的下标处开始搜索obj。
(3)public final int lastIndexOf(Object obj)
从向量尾部开始逆向搜索obj。
(4)public final synchronized int lastIndexOf(Object obj,int index)
从index所表示的下标处由尾至头逆向搜索obj。
(5)public final synchronized Object firstElement()
获取向量对象中的首个obj。
(6)public final synchronized Object lastElement()
获取向量对象中的最后一个obj。

实例

了解了向量的最基本的方法后,我们来看一下例子VectorApp.java。
例 VectorApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importjava.util.Vector;
importjava.lang.*;
//这一句不应该要,但原文如此
importjava.util.Enumeration;
public class VectorApp
{
    public static void main(String[]args)
    {
        Vector<Integer> v1=new Vector<Integer>();//jdk1.5以后增加了对的支持!
        Integer integer1=new Integer(1);
        /**
        *因为Vector<Integer>已经指定为Integer类型
        *当v1.addElement("one")时会报错,类型不符
        *ThemethodaddElement(Integer)inthetypeVector<Integer>isnotapplicablefor
        *thearguments(String)
        */
        //加入的为字符串对象
        v1.addElement("one");
        v1.addElement("two");
        //加入的为Integer的对象
        v1.addElement(integer1);
        v1.addElement(newInteger(2));
        System.out.println("Thevectorv1is:\n\t"+v1);
        //此处的输出结果为:Thevectorv1is:[1,2]
        /**
        *ThemethodinsertElementAt(Integer,int)inthetypeVector<Integer>isnotapplicablefor
        *thearguments(String,int)
        *insertElementAt()是指:Insertsthespecifiedobjectasacomponentinthisvectorat
        *thespecifiedindex.
        */
        //将v1转换成字符串并打印
        v1.insertElementAt("three",2);
        v1.insertElementAt(newFloat(3.9),3);
        System.out.println("Thevectorv1(usedmethodinsertElementAt())is:\n\t"+v1);
        //以上运行,请改Vector<Integer>为Vector<Float>
        /**
        *setElementAt(Eobj,intindex)的用法
        *Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject.
        *Thepreviouscomponentatthatpositionisdiscarded.              
        *Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector.
        */
        //将指定位置的对象设置为新的对象
        v1.setElementAt(2,1);
        System.out.println("Thevectorv1(usedmethodsetElementAt())is:\n\t"+v1);
        //从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始找,删除找到的第一个integer1
        v1.removeElement(integer1);
        //使用枚举类(Enumeration)的方法来获取向量对象的每个元素
        Enumerationenum=v1.elements();
        System.out.print("Thevectorv1(usedmethodremoveElement())is:");
        while(enum.hasMoreElements())
        System.out.print(enum.nextElement()+"");
        System.out.println();
        System.out.println("Thepositionofobject1(top-to-bottom):"
+v1.indexOf(integer1));
        System.out.println("Thepositionofobject1(tottom-to-top):"
+v1.lastIndexOf(integer1));
        //按不同的方向查找对象integer1所处的位置
        v1.setSize(4);
        System.out.println("Thenewvector(resizedthevector)is:"+v1);
        //重新设置v1的大小,多余的元素被行弃
    }
}

运行结果:
E:\java01>java VectorApp
The vector v1 is:
[one,1,1,two,2,1,1]
The vector v1(used method insertElementAt())is:
[one,1,three,3.9,1,two,2,1,1]
The vector v1(used method setElementAt()) is:
[one,1,four,3.9,1,two,2,1,1]
The vector v1(used method removeElement())is:
one four 3.9 1 two 2 1 1
The position of object 1(top-to-bottom):3
The position of object 1(tottom-to-top):7
The new vector(resized the vector)is:
[one,four,3.9,1]
E:\java01>
从例1中运行的结果中可以清楚地了解上面各种方法的作用,另外还有几点需解释。
(1)类Vector定义了方法
public final int size()
此方法用于获取向量元素的个数。它的返回值是向量中实际存在的元素个数,而非向量容量。可以调用方法capactly()来获取容量值。
方法:
public final synchronized void setsize(int newsize)
此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值,则超过部分的多余元素会丢失。
(2)程序中定义了Enumeration类的一个对象
Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法。
在Enumeration中提供了方法hasMoreElement()来判断集合中是否还有其它元素和方法nextElement()来获取下一个元素。利用这两个方法可以依次获得集合中元素。
Vector中提供方法:
public final synchronized Enumeration elements()
此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法,以便于用户获取对应的枚举类型。Java中,数组对象一旦创建后,其元素的个数 不能被修改。而Java.util包中的Vector类(向量)提供类似于数组的能力,且能够动态地调整自身的大小。Vector类似于一个数组,但与数组相比在使用上有两个优点:
① 使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加;
② Vector类提供额外的方法来增加、删除元素,比数组操作高效。[1]

插入功能

(1)public final synchronized void addElement(Object obj)
将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象,可在其中插入不同类的对象。但插入的应是对象而不是数值,所以插入数值时要注意将数值转换成相应的对象。
例 要插入一个整数1时,不要直接调用v1.addElement(1),正确的方法为:
1
2
3
Vectorv1=new Vector();
Integerinteger1=new Integer(1);
v1.addElement(integer1);

(2)public final synchronized void setElementAt(object obj,int index)
将index处的对象设成obj,原来的对象将被覆盖。
(3)public final synchronized void insertElementAt(Object obj,int index)
在index指定的位置插入obj,原来对象以及此后的对象依次往后顺延。

删除功能

(1)public final synchronized void removeElement(Object obj)
从向量中删除obj。若有多个存在,则从向量头开始试,删除找到的第一个与obj相同的向量成员。
(2)public final synchronized void removeAllElement()
删除向量中所有的对象。
(3)public final synchronized void removeElementlAt(int index)
删除index所指的地方的对象。

查询搜索功能

(1)public final int indexOf(Object obj)
从向量头开始搜索obj,返回所遇到的第一个obj对应的下标,若不存在此obj,返回-1。
(2)public final synchronized int indexOf(Object obj,int index)
从index所表示的下标处开始搜索obj。
(3)public final int lastIndexOf(Object obj)
从向量尾部开始逆向搜索obj。
(4)public final synchronized int lastIndexOf(Object obj,int index)
从index所表示的下标处由尾至头逆向搜索obj。
(5)public final synchronized Object firstElement()
获取向量对象中的首个obj。
(6)public final synchronized Object lastElement()
获取向量对象中的最后一个obj。

实例

了解了向量的最基本的方法后,我们来看一下例子VectorApp.java。
例 VectorApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importjava.util.Vector;
importjava.lang.*;
//这一句不应该要,但原文如此
importjava.util.Enumeration;
public class VectorApp
{
    public static void main(String[]args)
    {
        Vector<Integer> v1=new Vector<Integer>();//jdk1.5以后增加了对的支持!
        Integer integer1=new Integer(1);
        /**
        *因为Vector<Integer>已经指定为Integer类型
        *当v1.addElement("one")时会报错,类型不符
        *ThemethodaddElement(Integer)inthetypeVector<Integer>isnotapplicablefor
        *thearguments(String)
        */
        //加入的为字符串对象
        v1.addElement("one");
        v1.addElement("two");
        //加入的为Integer的对象
        v1.addElement(integer1);
        v1.addElement(newInteger(2));
        System.out.println("Thevectorv1is:\n\t"+v1);
        //此处的输出结果为:Thevectorv1is:[1,2]
        /**
        *ThemethodinsertElementAt(Integer,int)inthetypeVector<Integer>isnotapplicablefor
        *thearguments(String,int)
        *insertElementAt()是指:Insertsthespecifiedobjectasacomponentinthisvectorat
        *thespecifiedindex.
        */
        //将v1转换成字符串并打印
        v1.insertElementAt("three",2);
        v1.insertElementAt(newFloat(3.9),3);
        System.out.println("Thevectorv1(usedmethodinsertElementAt())is:\n\t"+v1);
        //以上运行,请改Vector<Integer>为Vector<Float>
        /**
        *setElementAt(Eobj,intindex)的用法
        *Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject.
        *Thepreviouscomponentatthatpositionisdiscarded.              
        *Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector.
        */
        //将指定位置的对象设置为新的对象
        v1.setElementAt(2,1);
        System.out.println("Thevectorv1(usedmethodsetElementAt())is:\n\t"+v1);
        //从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始找,删除找到的第一个integer1
        v1.removeElement(integer1);
        //使用枚举类(Enumeration)的方法来获取向量对象的每个元素
        Enumerationenum=v1.elements();
        System.out.print("Thevectorv1(usedmethodremoveElement())is:");
        while(enum.hasMoreElements())
        System.out.print(enum.nextElement()+"");
        System.out.println();
        System.out.println("Thepositionofobject1(top-to-bottom):"
+v1.indexOf(integer1));
        System.out.println("Thepositionofobject1(tottom-to-top):"
+v1.lastIndexOf(integer1));
        //按不同的方向查找对象integer1所处的位置
        v1.setSize(4);
        System.out.println("Thenewvector(resizedthevector)is:"+v1);
        //重新设置v1的大小,多余的元素被行弃
    }
}

运行结果:
E:\java01>java VectorApp
The vector v1 is:
[one,1,1,two,2,1,1]
The vector v1(used method insertElementAt())is:
[one,1,three,3.9,1,two,2,1,1]
The vector v1(used method setElementAt()) is:
[one,1,four,3.9,1,two,2,1,1]
The vector v1(used method removeElement())is:
one four 3.9 1 two 2 1 1
The position of object 1(top-to-bottom):3
The position of object 1(tottom-to-top):7
The new vector(resized the vector)is:
[one,four,3.9,1]
E:\java01>
从例1中运行的结果中可以清楚地了解上面各种方法的作用,另外还有几点需解释。
(1)类Vector定义了方法
public final int size()
此方法用于获取向量元素的个数。它的返回值是向量中实际存在的元素个数,而非向量容量。可以调用方法capactly()来获取容量值。
方法:
public final synchronized void setsize(int newsize)
此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值,则超过部分的多余元素会丢失。
(2)程序中定义了Enumeration类的一个对象
Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法。
在Enumeration中提供了方法hasMoreElement()来判断集合中是否还有其它元素和方法nextElement()来获取下一个元素。利用这两个方法可以依次获得集合中元素。
Vector中提供方法:
public final synchronized Enumeration elements()
此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法,以便于用户获取对应的枚举类型。

3. vector 是同一种类型的对象的集合,每个对象都有一个对应的整数索引值[2] 。

和 string 对象一样,标准库将负责管理与存储元素相关的内存。我们把 vector称为容器,是因为它可以包含其他对象,能够存放任意类型的动态数组,增加和压缩数据。一个容器中的所有对象都必须是同一种类型的[2]。
vector 是一个类模板(class template)。使用模板可以编写一个类定义或函数定义,而用于多个不同的数据类型。因此,我们可以定义保存 string 对象的 vector,或保存 int 值的 vector,又或是保存自定义的类类型对象(如Sales_items 对象)的 vector。vector 不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型。vector 类型的每一种都指定了其保存元素的类型

Vector:动态数组的使用和说明相关推荐

  1. C++ vector动态数组

    C++ vector动态数组 Vector投入是面向对象方式的动态数组 使用vector容器,可以轻松实现数组插入元素,vector可以轻松实现动态管理扩容 创建动态数组vector vector&l ...

  2. C++向量 vector动态数组

    需要包含头文件, #include  <vector>    using namespace std; vector 容器与数组相比其优点在于它能够根据需要随时自动调整自身的大小以便容下所 ...

  3. vector动态数组

    vector是C++中的一种数据结构,确切的说是一个类,它相当于一个动态的数组,当程序员无法知道自己需要的数组长度多大时,使用vector可以达到最大节约空间的目的,也就是实现动态分配数组. 举例如下 ...

  4. C++ STL :vector动态数组可实现整体赋值

    vector 是 STL 库中很常用的工具,可以理解为一个动态数组. vector可以实现整体赋值 普通的数组,比如 int 数组,只能通过索引一个元素一个元素的赋值,在有些情况下十分不便,但是今天在 ...

  5. vector 修改 java_java对vector动态数组中的对象排序,以下代码有何问题,如何修改?...

    展开全部 package com.tx.collection; import java.util.Comparator; import java.util.Iterator; import java. ...

  6. cpp中vector动态数组(一种container)的简单用法

    vector<int> num;for (int i=0; i<10; ++i)num.push_back(i);num.push_back(10);num.push_back(10 ...

  7. C++动态数组vector实现

    最近在做将MATLAB代码转化为C/C++的工作,在实际应用时,发现动态数组非常重要,我在学习的时候也踩了许多坑,这篇就当做一篇踩坑笔记,希望读者能够绕开我踩过的坑,顺利应用动态数组. 1.静态数组. ...

  8. Rust学习教程32 - 动态数组Vec

    本文节选自<<Rust语言圣经>>一书 欢迎大家加入Rust编程学院,一起学习交流: QQ群:1009730433 动态数组Vector 动态数组类型用Vec<T> ...

  9. vector,数组,动态数组效率测试

    对vector.数组.new创建的动态数组.预先reverse的vector测试代码如下: #include <iostream> #include <vector> #inc ...

  10. vector 容器 动态数组总结

    vector 容器 动态数组总结 二话不说直接上代码 #include <vector> #include <algorithm> #include <iostream& ...

最新文章

  1. 2021年大数据ELK(十三):Elasticsearch编程(添加职位数据)
  2. Scratch等级考试(二级)模拟题
  3. 空间统计笔记之一(基础知识)
  4. 优贝共享数据交易所网_2020.9.9号币圈简报:水天共享数据迁移,链淘公告明天开始减产...
  5. 前端学习(3256):react中添加todolist
  6. 爬虫实战学习笔记_1 爬虫基础+HTTP原理
  7. 02-Django基础知识
  8. JavaScript 01
  9. 知识图谱组队学习Task05——图数据库查询
  10. esp32FreeRTOS教程——内核分配
  11. python 爬虫抓取网页数据导出excel_Python实现抓取网页生成Excel文件的方法示例
  12. 第2章:知识表示--实践:Protégé本体构建
  13. 在WEB项目中调用QQ通讯组件打开QQ聊天界面
  14. AABB和OBB包围盒简介
  15. 关于“微笑涛声”博客
  16. 国开教育学形考任务2试题1试题及答案
  17. 目前最好的python教程_目和毫米的换算
  18. C#窗口程序 UI模板【简约风、去边框】
  19. 渗透测试之---xss-labs闯关【1-14关】
  20. VMware设置虚拟机与物理主机处于同一网段,桥接模式

热门文章

  1. 实现二分归并排序算法_如何实现归并排序?
  2. REVERSE-PRACTICE-CTFSHOW-5
  3. java mediator_java—mediator中介模式
  4. 【NC14 按之字形顺序打印二叉树】
  5. 【BZOJ - 1059】矩阵游戏(二分图匹配,建图,最小边覆盖)
  6. 【nyoj - 860】 又见0-1背包 (dp,反向0-1背包,好题好思路)
  7. 【51NOD - 1523】 非回文(dfs)
  8. 6.深度学习练习:Initialization
  9. layui数据表格获取当前页数
  10. leetcode238 除本身以外数组的乘积