Java Vector

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java Vector

笑霸final   2022-05-23 我要评论

Vector的基本介绍

1.:Vector类的定义:

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

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

protected Object[] elementData;

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

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

Vector 类支持 4 种构造方法

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

public Vector() {
        this(10);
    }

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

public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

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

/**
     * 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 元素的向量:

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);

代码

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());
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们