国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

java.util包

2019-11-18 11:33:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

   ArrayList是List接口的一個(gè)可變長(zhǎng)數(shù)組實(shí)現(xiàn)。實(shí)現(xiàn)了所有List接口的操作,并答應(yīng)存儲(chǔ)null值。除了沒(méi)有進(jìn)行同步,ArrayList基本等同于Vector。在Vector中幾乎對(duì)所有的方法都進(jìn)行了同步,但ArrayList僅對(duì)writeObject和readObject進(jìn)行了同步,其它比如add(Object)、remove(int)等都沒(méi)有同步。

1.存儲(chǔ)
ArrayList使用一個(gè)Object的數(shù)組存儲(chǔ)元素。
PRivate transient Object elementData[];
ArrayList實(shí)現(xiàn)了java.io.Serializable接口,這兒的transient標(biāo)示這個(gè)屬性不需要自動(dòng)序列化。下面會(huì)在writeObject()方法中具體講解為什么要這樣作。

2.add和remove


    public boolean add(Object o) { 
    ensureCapacity(size + 1);  // Increments modCount!! 
    elementData[size++] = o; 
    return true; 
    } 

注重這兒的ensureCapacity()方法,它的作用是保證elementData數(shù)組的長(zhǎng)度可以容納一個(gè)新元素。在“自動(dòng)變長(zhǎng)機(jī)制”中將具體講解。

    public Object remove(int index) { 
    RangeCheck(index); 
    modCount++; 
    Object oldValue = elementData[index]; 
    int numMoved = size - index - 1; 
    if (numMoved > 0) 
        System.arraycopy(elementData, index+1, elementData, index, 
                 numMoved); 
    elementData[--size] = null; // Let gc do its work 
    return oldValue; 
    } 

RangeCheck()的作用是進(jìn)行邊界檢查。由于ArrayList采用一個(gè)對(duì)象數(shù)組存儲(chǔ)元素,所以在刪除一個(gè)元素時(shí)需要把后面的元素前移。刪除一個(gè)元素時(shí)只是把該元素在elementData數(shù)組中的引用置為null,具體的對(duì)象的銷(xiāo)毀由垃圾收集器負(fù)責(zé)。
modCount的作用將在下面的“iterator()中的同步”中說(shuō)明。
注:在前移時(shí)使用了System提供的一個(gè)實(shí)用方法:arraycopy(),在本例中可以看出System.arraycopy()方法可以對(duì)同一個(gè)數(shù)組進(jìn)行操作,這個(gè)方法是一個(gè)native方法,假如對(duì)同一個(gè)數(shù)組進(jìn)行操作時(shí),會(huì)首先把從源部分拷貝到一個(gè)臨時(shí)數(shù)組,在把臨時(shí)數(shù)組的元素拷貝到目標(biāo)位置。

3.自動(dòng)變長(zhǎng)機(jī)制
在實(shí)例化一個(gè)ArrayList時(shí),你可以指定一個(gè)初始容量。這個(gè)容量就是elementData數(shù)組的初始長(zhǎng)度。假如你使用:

    ArrayList list = new ArrayList(); 

則使用缺省的容量:10。

    public ArrayList() { 
    this(10); 
    } 

ArrayList提供了四種add()方法,

public boolean add(Object o)

public void add(int index, Object element)

public boolean addAll(Collection c)

public boolean addAll(int index, Collection c)

在每一種add()方法中,都首先調(diào)用了一個(gè)ensureCapacity(int miniCapacity)方法,這個(gè)方法保證elementData數(shù)組的長(zhǎng)度不小于miniCapacity。ArrayList的自動(dòng)變長(zhǎng)機(jī)制就是在這個(gè)方法中實(shí)現(xiàn)的。

    public void ensureCapacity(int minCapacity) { 
    modCount++; 
    int oldCapacity = elementData.length; 
    if (minCapacity > oldCapacity) { 
        Object oldData[] = elementData; 
        int newCapacity = (oldCapacity * 3)/2 + 1; 
            if (newCapacity < minCapacity) 
        newCapacity = minCapacity; 
        elementData = new Object[newCapacity]; 
        System.arraycopy(oldData, 0, elementData, 0, size); 
    } 
    } 

從這個(gè)方法實(shí)現(xiàn)中可以看出ArrayList每次擴(kuò)容,都擴(kuò)大到原來(lái)大小的1.5倍。
每種add()方法的實(shí)現(xiàn)都大同小異,下面給出add(Object)方法的實(shí)現(xiàn):

    public boolean add(Object o) { 
    ensureCapacity(size + 1);  // Increments modCount!! 
    elementData[size++] = o; 
    return true; 
    } 


4.iterator()中的同步
在父類(lèi)AbstractList中定義了一個(gè)int型的屬性:modCount,記錄了ArrayList結(jié)構(gòu)性變化的次數(shù)。

    protected transient int modCount = 0; 

在ArrayList的所有涉及結(jié)構(gòu)變化的方法中都增加modCount的值,包括:add()、remove()、addAll()、removeRange()及clear()方法。這些方法每調(diào)用一次,modCount的值就加1。
注:add()及addAll()方法的modCount的值是在其中調(diào)用的ensureCapacity()方法中增加的。

AbstractList中的iterator()方法(ArrayList直接繼續(xù)了這個(gè)方法)使用了一個(gè)私有內(nèi)部成員類(lèi)Itr,生成一個(gè)Itr對(duì)象(Iterator接口)返回:

    public Iterator iterator() { 
    return new Itr(); 
    } 

Itr實(shí)現(xiàn)了Iterator()接口,其中也定義了一個(gè)int型的屬性:eXPectedModCount,這個(gè)屬性在Itr類(lèi)初始化時(shí)被賦予ArrayList對(duì)象的modCount屬性的值。

    int expectedModCount = modCount; 

注:內(nèi)部成員類(lèi)Itr也是ArrayList類(lèi)的一個(gè)成員,它可以訪問(wèn)所有的AbstractList的屬性和方法。理解了這一點(diǎn),Itr類(lèi)的實(shí)現(xiàn)就輕易理解了。

在Itr.hasNext()方法中:

    public boolean hasNext() { 
        return cursor != size(); 
    } 

調(diào)用了AbstractList的size()方法,比較當(dāng)前光標(biāo)位置是否越界。

在Itr.next()方法中,Itr也調(diào)用了定義在AbstractList中的get(int)方法,返回當(dāng)前光標(biāo)處的元素:

    public Object next() { 
        try { 
        Object next = get(cursor); 
        checkForComodification(); 
        lastRet = cursor++; 
        return next; 
        } catch(IndexOutOfBoundsException e) { 
        checkForComodification(); 
        throw new NoSUChElementException(); 
        } 
    } 

注重,在next()方法中調(diào)用了checkForComodification()方法,進(jìn)行對(duì)修改的同步檢查:

    final void checkForComodification() { 
        if (modCount != expectedModCount) 
        throw new ConcurrentModificationException(); 
    } 

現(xiàn)在對(duì)modCount和expectedModCount的作用應(yīng)該非常清楚了。在對(duì)一個(gè)集合對(duì)象進(jìn)行跌代操作的同時(shí),并不限制對(duì)集合對(duì)象的元素進(jìn)行操作,這些操作包括一些可能引起跌代錯(cuò)誤的add()或remove()等危險(xiǎn)操作。在AbstractList中,使用了一個(gè)簡(jiǎn)單的機(jī)制來(lái)規(guī)避這些風(fēng)險(xiǎn)。這就是modCount和expectedModCount的作用所在。

5.序列化支持
ArrayList實(shí)現(xiàn)了java.io.Serializable接口,所以ArrayList對(duì)象可以序列化到持久存儲(chǔ)介質(zhì)中。ArrayList的主要屬性定義如下:

private static final long serialVersionUID = 8683452581122892189L;

private transient Object elementData[];

private int size;

可以看出serialVersionUID和size都將自動(dòng)序列化到介質(zhì)中,但elementData數(shù)組對(duì)象卻定義為transient了。也就是說(shuō)ArrayList中的所有這些元素都不會(huì)自動(dòng)系列化到介質(zhì)中。為什么要這樣實(shí)現(xiàn)?因?yàn)閑lementData數(shù)組中存儲(chǔ)的“元素”其實(shí)僅是對(duì)這些元素的一個(gè)引用,并不是真正的對(duì)象,序列化一個(gè)對(duì)象的引用是毫無(wú)意義的,因?yàn)樾蛄谢菫榱朔葱蛄谢?dāng)你反序列化時(shí),這些對(duì)象的引用已經(jīng)不可能指向原來(lái)的對(duì)象了。所以在這兒需要手工的對(duì)ArrayList的元素進(jìn)行序列化操作。這就是writeObject()的作用。

    private synchronized void writeObject(java.io.ObjectOutputStream s) 
        throws java.io.IOException{ 
    // Write out element count, and any hidden stuff 
    s.defaultWriteObject(); 
   // Write out array length 
    s.writeInt(elementData.length); 
    // Write out all elements in the proper order. 
    for (int i=0; i<size; i++) 
            s.writeObject(elementData[i]); 
    } 

這樣元素?cái)?shù)組elementData中的所以元素對(duì)象就可以正確地序列化到存儲(chǔ)介質(zhì)了。
對(duì)應(yīng)的readObject()也按照writeObject()方法的順序從輸入流中讀取:

    private synchronized void readObject(java.io.ObjectInputStream s) 
        throws java.io.IOException, ClassNotFoundException { 
    // Read in size, and any hidden stuff 
    s.defaultReadObject(); 
    // Read in array length and allocate array 
    int arrayLength = s.readInt(); 
    elementData = new Object[arrayLength]; 
    // Read in all elements in the proper order. 
    for (int i=0; i<size; i++) 
            elementData[i] = s.readObject(); 
    } 



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 太仆寺旗| 怀来县| 西乌| 古浪县| 专栏| 关岭| 潍坊市| 忻州市| 上饶市| 梁山县| 吴堡县| 如东县| 镇江市| 浦城县| 寿光市| 麻城市| 阳新县| 克什克腾旗| 北辰区| 晴隆县| 错那县| 荔波县| 香格里拉县| 揭东县| 农安县| 中江县| 雷山县| 墨脱县| 噶尔县| 台江县| 三原县| 兴安县| 永春县| 平定县| 高雄市| 新余市| 桃园县| 襄汾县| 霍邱县| 磐石市| 邢台县|