亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java的List集合框架之Vector詳細(xì)解析

 更新時(shí)間:2023年11月16日 09:53:25   作者:進(jìn)擊的貓  
這篇文章主要介紹了Java的List集合框架之Vector詳細(xì)解析,List接口繼承Collection,Collection繼承于Iterable,List接口實(shí)現(xiàn)類分為Vector、ArrayList、LinkedList,Vector底層是一個(gè)Object數(shù)組,需要的朋友可以參考下

(一)List子父層級(jí):

Vector繼承關(guān)系

  • List接口繼承Collection,Collection繼承于Iterable;
  • List接口實(shí)現(xiàn)類分為:Vector、ArrayList、LinkedList;

(二)List實(shí)現(xiàn)類——Vector

1、Vector實(shí)現(xiàn)類

(1)Vector底層是一個(gè)Object數(shù)組,protected Object[] elementData;

(2)Vector中的所有涉及到Object數(shù)組的均是會(huì)加synchronized,列舉常見方法:構(gòu)造方法、add、remove、get、set等

(3)Vector中的默認(rèn)容量為10,且擴(kuò)容默認(rèn)為當(dāng)前容量翻倍(直接相加或者構(gòu)造方法中指定擴(kuò)容大小)。

2、常見源碼:

(1)構(gòu)造方法:

//無(wú)參構(gòu)造
 public Vector() {
        this(10);//默認(rèn)大小為10
    }

//初始化指定大小的有參構(gòu)造方法
        public Vector(int initialCapacity) {
            this(initialCapacity, 0);
        }

//指定擴(kuò)容大小和初始值的有參構(gòu)造   
public Vector(int initialCapacity, int capacityIncrement) {
    super();//父類無(wú)參構(gòu)造
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];//實(shí)例化存儲(chǔ)數(shù)組
    this.capacityIncrement = capacityIncrement;//設(shè)置自動(dòng)擴(kuò)容大小
}

(2)add方法:

/*
* 此處列舉的直接末尾添加add方法,還有指定位置添加insertElementAt方法添加,操作數(shù)組均會(huì)在方法上添加synchronized鎖
**/
public synchronized boolean add(E e) {
    modCount++;//操作次數(shù)
    ensureCapacityHelper(elementCount + 1);//是否需要擴(kuò)容
    elementData[elementCount++] = e;//存儲(chǔ)數(shù)據(jù)
    return true;
}
//擴(kuò)容判定
private void ensureCapacityHelper(int minCapacity) {
 if (minCapacity - elementData.length > 0)//判定是否需要擴(kuò)容,當(dāng)前存入值已大于數(shù)組大小
     grow(minCapacity);//調(diào)用擴(kuò)容方法
}

//數(shù)組最大值,使用Integer最大值2147483648-8,因數(shù)組需要存自身的元數(shù)據(jù)占用8位(Class\Lock\Flag\Size)
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

//擴(kuò)容方法
private void grow(int minCapacity) {
    int oldCapacity = elementData.length;//當(dāng)前數(shù)組大小
    //根據(jù)構(gòu)造函數(shù)判定擴(kuò)容值,未指定則采用翻一倍方式擴(kuò)容
    int newCapacity = oldCapacity + ((capacityIncrement > 0)?capacityIncrement : oldCapacity);
    if (newCapacity - minCapacity < 0)//判定新擴(kuò)容值是否滿足當(dāng)前存入后的數(shù)組大小
        newCapacity = minCapacity;//擴(kuò)容大小不滿足,則直接擴(kuò)容到滿足存入后的數(shù)組大小
    if (newCapacity - MAX_ARRAY_SIZE > 0)//判定擴(kuò)容后的值是否大于數(shù)組最大值
        newCapacity = hugeCapacity(minCapacity);
    elementData = Arrays.copyOf(elementData, newCapacity);//數(shù)組遷移復(fù)制
}
//存儲(chǔ)最大值判定,超出限制則默認(rèn)使用最大值
private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) //越界
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE)?Integer.MAX_VALUE:MAX_ARRAY_SIZE;//返回最大值或者數(shù)組最大值
}

(3)remove方法:

/*
* 此處列舉的返回boolean值的刪除方法,還有直接刪除指定索引的remove,
* 它直接在方法上添加synchronized鎖,邏輯與具體刪除一致
**/
public boolean remove(Object o) {
    return removeElement(o);//調(diào)用具體刪除方法
}

public synchronized boolean removeElement(Object obj) {
    modCount++;//操作次數(shù)
    int i = indexOf(obj);//獲取obj在數(shù)組中存儲(chǔ)的下標(biāo),防止數(shù)組越界
    if (i >= 0) {
        removeElementAt(i);//調(diào)用刪除方法
        return true;
    }
    return false;
}
//索引尋找
public int indexOf(Object o) {
    return indexOf(o, 0);//傳入需要比對(duì)值和數(shù)組首標(biāo)
}
//遍歷數(shù)組尋找指定值(找尋下標(biāo),防止線程不安全,加synchronized)
public synchronized int indexOf(Object o, int index) {
    if (o == null) {//判定傳入是否為空
        for (int i = index ; i < elementCount ; i++)
            if (elementData[i]==null)
                return i;//返回?cái)?shù)組下標(biāo)第一個(gè)為null的索引
    } else {
        for (int i = index ; i < elementCount ; i++)
            if (o.equals(elementData[i]))//判定需要?jiǎng)h除值與數(shù)組值是否相等,相等則返回第一個(gè)相等的下標(biāo)
                return i;//返回索引值
    }
    return -1;
}
//根據(jù)索引對(duì)指定下標(biāo)進(jìn)行刪除(與存儲(chǔ)數(shù)組進(jìn)行交互,添加synchronized鎖)
public synchronized void removeElementAt(int index) {
    modCount++;//操作次數(shù)
    if (index >= elementCount) {//越界判定
        throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                 elementCount);
    }else if (index < 0) {//越界判定
        throw new ArrayIndexOutOfBoundsException(index);
    }
    int j = elementCount - index - 1;//計(jì)算需要被遷移的數(shù)組成員數(shù)量
    if (j > 0) {
        //使用系統(tǒng)類提供的數(shù)組復(fù)制方法進(jìn)行遷移復(fù)制
        //參數(shù)說(shuō)明:被復(fù)制的數(shù)組,被復(fù)制的數(shù)組起點(diǎn)下標(biāo),目標(biāo)數(shù)組(復(fù)制到的新數(shù)組),目標(biāo)數(shù)組被復(fù)制的起點(diǎn)下標(biāo),需要復(fù)制的大小
        System.arraycopy(elementData, index + 1, elementData, index, j);
    }
    elementCount--;//成員數(shù)量減1
    //將原來(lái)存儲(chǔ)有值的最后一位值置為空,便于GC回收工作,提高內(nèi)存使用率
    elementData[elementCount] = null;
}

(4)get方法:

public synchronized E get(int index) {
     if (index >= elementCount)//越界控制
         throw new ArrayIndexOutOfBoundsException(index);
     return elementData(index);
 }
 //返回指定數(shù)組值
 E elementData(int index) {
     return (E) elementData[index];
 }

(5)set方法:

public synchronized E set(int index, E element) {
   if (index >= elementCount)//越界控制
        throw new ArrayIndexOutOfBoundsException(index);
    E oldValue = elementData(index);//獲取索引已存在的值或者null
    elementData[index] = element;//設(shè)置新值
    return oldValue;//返回設(shè)置之前的索引數(shù)組值
}

3、總結(jié)

(1)Vector是線程安全的,每個(gè)涉及到存儲(chǔ)數(shù)據(jù)的方法都添加synchronized鎖,性能相對(duì)會(huì)較低;

(2)Vector默認(rèn)的存儲(chǔ)大小為10,不指定自動(dòng)擴(kuò)容大小,則默認(rèn)使用當(dāng)前數(shù)組大小進(jìn)行自加擴(kuò)容(直接相加或者構(gòu)造方法中指定擴(kuò)容大?。?      

到此這篇關(guān)于Java的List集合框架之Vector詳細(xì)解析的文章就介紹到這了,更多相關(guān)List集合框架之Vector內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論