最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java快速掌握Vector類方法

 更新時(shí)間:2022年03月04日 15:51:25   作者:笑霸final  
Vector?類實(shí)現(xiàn)了一個(gè)動(dòng)態(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:底層也是一個(gè)對(duì)象數(shù)組

protected Object[] elementData;

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

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

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

1 第一種構(gòu)造方法創(chuàng)建一個(gè)默認(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 指定。增量表示向量每次增加的元素?cái)?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向量溢出時(shí)容量增加的量 
     * @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)建一個(gè)包含集合 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可以存入一個(gè)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.移除此向量中指定元素的第一個(gè)匹配項(xiàng),如果向量不包含該元素,
則元素保持不變。
boolean remove(Object o)
3.從此向量中移除包含在指定 Collection 中的所有元素。
boolean removeAll(Collection c)

3.set方法

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

4.size、capacity、get方法

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

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

get 返回第幾個(gè)的內(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);//可以存一個(gè)null
        System.out.println("\n"+"組件數(shù)="+vector.size());
        System.out.println("容量="+vector.capacity());
    }
}

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

相關(guān)文章

最新評(píng)論

闽清县| 河津市| 右玉县| 界首市| 民乐县| 邳州市| 新干县| 田东县| 曲麻莱县| 岳阳县| 桃江县| 梅州市| 资中县| 临高县| 新田县| 莱西市| 兴山县| 绥中县| 巍山| 南木林县| 安泽县| 乌鲁木齐市| 耒阳市| 承德市| 巴中市| 五常市| 琼中| 湄潭县| 洪泽县| 磐石市| 天柱县| 巴林右旗| 遵化市| 韶关市| 隆林| 天等县| 玛纳斯县| 焦作市| 溧阳市| 东阿县| 万山特区|