溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ArrayList有什么用

發(fā)布時(shí)間:2021-12-18 14:20:51 來(lái)源:億速云 閱讀:98 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“ ArrayList有什么用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

ArrayList是一個(gè)有序列表。

public class ArrayList<E> extends AbstractList<E>
     implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  • RandomAccess 標(biāo)識(shí)接口,無(wú)內(nèi)部方法,表示可以隨機(jī)訪問(wèn)內(nèi)部元素

  • Cloneable 標(biāo)識(shí)接口,無(wú)內(nèi)部方法,表示可以進(jìn)行負(fù)責(zé)

  • java.io.Serializable 標(biāo)識(shí)接口,無(wú)內(nèi)部方法,表示支持序列化

底層數(shù)據(jù)結(jié)構(gòu)是一個(gè)對(duì)象數(shù)組

transient Object[] elementData;

ArrayList對(duì)象有存儲(chǔ)上限

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

縮容,當(dāng)buffer分配的空間容量大于實(shí)際存儲(chǔ)的元素?cái)?shù)量時(shí),通過(guò)縮容釋放對(duì)于的資源。

public void trimToSize() {
    modCount++;
    if (size < elementData.length) {
        elementData = (size == 0)
          ? EMPTY_ELEMENTDATA
          : Arrays.copyOf(elementData, size);
    }
}

擴(kuò)容,步長(zhǎng)為原容量的 50%, 擴(kuò)容后將原buffer復(fù)制到新創(chuàng)建的buffer中。所以ArrayList如果存在頻繁擴(kuò)容的情況,會(huì)引起程序性能下降。

private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
}

插入或刪除指定位置的Element,都會(huì)引起底層數(shù)據(jù)的批量復(fù)制

插入

public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

刪除

 public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;
}


private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
}

清空數(shù)據(jù)時(shí),并不釋放底層數(shù)組分配的空間,只是將數(shù)組中的對(duì)象持有復(fù)制成null,方便虛擬機(jī)在GC時(shí),直接將對(duì)象內(nèi)存釋放。

public void clear() {
    modCount++;

    // clear to let GC do its work
    // 釋放句柄,告訴GC對(duì)象可以被回收,但并不是立即回收
    for (int i = 0; i < size; i++)
        elementData[i] = null;

    size = 0;
}

在數(shù)據(jù)計(jì)算中存在集合求交集運(yùn)算,在ArrayList中同樣提供了該方法。

求交集

public boolean retainAll(Collection<?> c) {
    Objects.requireNonNull(c);
    return batchRemove(c, true);
}

private boolean batchRemove(Collection<?> c, boolean complement) {
    final Object[] elementData = this.elementData;
    int r = 0, w = 0;
    boolean modified = false;
    try {
        for (; r < size; r++)
            if (c.contains(elementData[r]) == complement)
                elementData[w++] = elementData[r];
    } finally {
        // Preserve behavioral compatibility with AbstractCollection,
        // even if c.contains() throws.
        if (r != size) {
            System.arraycopy(elementData, r,
                             elementData, w,
                             size - r);
            w += size - r;
        }
        if (w != size) {
            // clear to let GC do its work
            for (int i = w; i < size; i++)
                elementData[i] = null;
            modCount += size - w;
            size = w;
            modified = true;
        }
    }
    return modified;
}

ArrayList實(shí)現(xiàn)了Serializable接口,表示它可以被序列化,在其內(nèi)部提供了jdk標(biāo)準(zhǔn)的序列化和反序列號(hào)的方法。通過(guò)序列化和發(fā)序列化方法可以發(fā)現(xiàn),JDK并不是把ArrayList所有屬性都寫入文件,只是將size,以及數(shù)組中的每一個(gè)對(duì)象進(jìn)行了序列化。

private void writeObject(java.io.ObjectOutputStream s)
    throws java.io.IOException{
    // Write out element count, and any hidden stuff
    int expectedModCount = modCount;
    s.defaultWriteObject();

    // Write out size as capacity for behavioural compatibility with clone()
    s.writeInt(size);

    // Write out all elements in the proper order.
    for (int i=0; i<size; i++) {
        s.writeObject(elementData[i]);
    }

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


private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {
    elementData = EMPTY_ELEMENTDATA;

    // Read in size, and any hidden stuff
    s.defaultReadObject();

    // Read in capacity
    s.readInt(); // ignored

    if (size > 0) {
        // be like clone(), allocate array based upon size not capacity
        ensureCapacityInternal(size);

        Object[] a = elementData;
        // Read in all elements in the proper order.
        for (int i=0; i<size; i++) {
            a[i] = s.readObject();
        }
    }
}

“ ArrayList有什么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI