对象 ArrayList 中 contains(Object o) 的时间复杂度

发布于 2024-11-03 16:18:18 字数 310 浏览 2 评论 0原文

正如标题所说,我想知道 ArrayList 是。

As the title says, I was wondering what the time complexity of the contains() method of an ArrayList is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小…楫夜泊 2024-11-10 16:18:18
O(n)

sizeisEmptygetsetiteratorlistIterator 操作以恒定时间运行。 add 操作以摊销的恒定时间运行,也就是说,添加 n 个元素需要 O(n) 时间。所有其他操作都以线性时间运行(粗略地说)。与 LinkedList 实现相比,常数因子较低。

http://download.oracle.com/javase/6 /docs/api/java/util/ArrayList.html

O(n)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

最近可好 2024-11-10 16:18:18

ArrayList 的 O(n)

it's O(n) for ArrayList

撩心不撩汉 2024-11-10 16:18:18

如果您查看 ArrayList 的源代码并检查其 contains 方法,它看起来如下所示:

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

contains 将检查委托给 indexOf方法。因此,如果我们检查 indexOf 实现,它如下所示:

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

从代码中可以看出,为了找到给定元素的索引,在最坏的情况下,必须迭代整个阵列。随着数组大小的增加,元素的搜索时间也会增加。因此,contains方法的时间复杂度为O(n),其中n是列表中元素的数量。

If you look into the source code for ArrayList and check its contains method it looks as below:

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

contains delegates the check to the indexOf method. So, if we check the indexOf implementation it is as follows:

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

As it can be seen from the code, in order to find an index of a given element, one, in the worst case, must iterate through the whole array. As a size of the array grows and so does the search time by an element. Hence, the time complexity of contains method is O(n), where n is the number of elements in the list.

骄兵必败 2024-11-10 16:18:18

它的O(n)contains(Object o)indexOf() 上实现,需要 O(n)。因此 contains(Object o) 的复杂性是防御性的 O(n)

如果您需要的话,这里还有其他一些:

add() - O(1)
add(index, element) – O(n)
get() – O(1)
set() – O(1)
remove() –  (n)
indexOf()` – O(n)

its O(n). contains(Object o) is implemented on indexOf() which takes O(n). So complexity of contains(Object o) is defensively O(n)

Here are some other if you need:

add() - O(1)
add(index, element) – O(n)
get() – O(1)
set() – O(1)
remove() –  (n)
indexOf()` – O(n)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文