服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java快速掌握Vector类方法

Java快速掌握Vector类方法

2022-08-27 11:48笑霸final Java教程

Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:Vector 是同步访问的;Vector 包含了许多传统的方法,这些方法不属于集合框架

Vector的基本介绍

1.:Vector类的定义:

?
1
2
3
public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

2:底层也是一个对象数组

?
1
protected Object[] elementData;

3:Vector是线程同步的,即线程安全,Vector类带有操作方法有synchronized

4:在开发中,需要线程安全时,考虑Vector

Vector 类支持 4 种构造方法

1 第一种构造方法创建一个默认的向量,默认大小为 10:

?
1
2
3
public Vector() {
        this(10);
    }

第二种构造方法创建指定大小的向量。

?
1
2
3
public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

第三种构造方法创建指定大小的向量,并且增量用 capacityIncrement 指定。增量表示向量每次增加的元素数目。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
     * Constructs an empty vector with the specified initial capacity and
     * capacity increment.
     *
     * @param   initialCapacity     the initial capacity of the vector
     * @param   capacityIncrement   the amount by which the capacity is
     *  increased when the vector overflows向量溢出时容量增加的量
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

第四种构造方法创建一个包含集合 c 元素的向量:

?
1
2
3
4
5
6
7
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);
    }

一些常用的方法

1.add方法

注意:add可以存入一个null;详见size放法


1.将指定元素添加到此向量的末尾。
boolean add(Object o)
2.在此向量的指定位置插入指定的元素。
void add(int index, Object element)
3.将指定 Collection 中的所有元素添加到此向量的末尾,
按照指定 collection 的迭代器所返回的顺序添加这些元素。
boolean addAll(Collection c)
4.在指定位置将指定 Collection 中的所有元素插入到此向量中。
boolean addAll(int index, Collection c)

2.remove方法

1.移除此向量中指定位置的元素。
Object remove(int index)
2.移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,
则元素保持不变。
boolean remove(Object o)
3.从此向量中移除包含在指定 Collection 中的所有元素。
boolean removeAll(Collection c)

3.set方法

1.用指定的元素替换此向量中指定位置处的元素。
Object set(int index, Object element)
2.将此向量指定 index 处的组件设置为指定的对象
void setElementAt(Object obj, int index)

4.size、capacity、get方法

size返回此向量中的组件数(就是向量存是对象的数量)。

capacity 返回此向量的当前容量。

get 返回第几个的内容

int size();
int capacity();
Object get(int index);

Java快速掌握Vector类方法

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Vector;
 
/**
 * @autor 笑霸fianl~
 * 欢迎访问GitHub:https://github.com/XBfinal
 * 欢迎访问Gitee:https://gitee.com/XBfianl
 * 欢迎访问CSDN:https://blog.csdn.net/weixin_52062043
 */
public class enumeration01 {
    public static void main(String[] args) {
        Vector vector = new Vector();
        for(int i=0;i<10;i++){
            vector.add(i);
        }
        for(int i=0;i<10;i++){
            System.out.print(vector.get(i)+"\t");
        }
        vector.add(null);//可以存一个null
        System.out.println("\n"+"组件数="+vector.size());
        System.out.println("容量="+vector.capacity());
    }
}

到此这篇关于Java快速掌握Vector类方法的文章就介绍到这了,更多相关Java Vector内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_52062043/article/details/122814452

延伸 · 阅读

精彩推荐