簡體   English   中英

在 ArrayList 中,如果使用 remove() 在索引 0 處刪除元素,那么時間復雜度是多少?

[英]In an ArrayList if an element is removed at index 0 using remove(), then what will be the time complexity?

我認為時間復雜度將是 O(1)。 由於 remove 方法的聲明中沒有循環。 如果我考慮時間復雜度的方法不正確,請告訴我。

remove() 的聲明:

public synchronized E remove(int index) 
{
    modCount++;
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    E oldValue = elementData(index);

    int numMoved = elementCount - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--elementCount] = null; // Let gc do its work

    return oldValue;
}

這是不正確的。 那里有一個循環,即使您沒有看到“for”或“while”這個詞。 (提示:該函數內部調用的所有函數的時間復雜度是多少?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM