欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java快速掌握Vector類方法

 更新時間:2022年03月04日 15:51:25   作者:笑霸final  
Vector?類實(shí)現(xiàn)了一個動態(tài)數(shù)組。和?ArrayList?很相似,但是兩者是不同的:Vector?是同步訪問的;Vector?包含了許多傳統(tǒng)的方法,這些方法不屬于集合框架

Vector的基本介紹

1.:Vector類的定義:

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

2:底層也是一個對象數(shù)組

protected Object[] elementData;

3:Vector是線程同步的,即線程安全,Vector類帶有操作方法有synchronized

4:在開發(fā)中,需要線程安全時,考慮Vector

Vector 類支持 4 種構(gòu)造方法

1 第一種構(gòu)造方法創(chuàng)建一個默認(rèn)的向量,默認(rèn)大小為 10:

public Vector() {
        this(10);
    }

第二種構(gòu)造方法創(chuàng)建指定大小的向量。

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

第三種構(gòu)造方法創(chuàng)建指定大小的向量,并且增量用 capacityIncrement 指定。增量表示向量每次增加的元素數(shù)目。

/**
     * 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;
    }

第四種構(gòu)造方法創(chuàng)建一個包含集合 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.移除此向量中指定元素的第一個匹配項(xiàng),如果向量不包含該元素,
則元素保持不變。
boolean remove(Object o)
3.從此向量中移除包含在指定 Collection 中的所有元素。
boolean removeAll(Collection c)

3.set方法

1.用指定的元素替換此向量中指定位置處的元素。
Object set(int index, Object element)
2.將此向量指定 index 處的組件設(shè)置為指定的對象
void setElementAt(Object obj, int index)

4.size、capacity、get方法

size返回此向量中的組件數(shù)(就是向量存是對象的數(shù)量)。

capacity 返回此向量的當(dāng)前容量。

get 返回第幾個的內(nèi)容

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"+"組件數(shù)="+vector.size());
        System.out.println("容量="+vector.capacity());
    }
}

到此這篇關(guān)于Java快速掌握Vector類方法的文章就介紹到這了,更多相關(guān)Java Vector內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論