java ArrayList 的时间复杂度

发布于 2024-08-20 03:08:37 字数 101 浏览 2 评论 0原文

java中的ArrayList是数组还是列表? get 操作的时间复杂度是多少,是 O(n) 还是 O(1)

Is ArrayList an array or a list in java? what is the time complexity for the get operation, is it O(n) or O(1)?

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

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

发布评论

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

评论(6

栀子花开つ 2024-08-27 03:08:37

Java 中的ArrayList 是一个由数组 支持的List

get(index) 方法是一个常数时间、O(1) 的操作。

直接来自 Java 库的 ArrayList.get(index) 代码:

public E get(int index) {
    RangeCheck(index);
    return (E) elementData[index];
}

基本上,它只是直接从支持数组中返回一个值。 (RangeCheck(index)) 也是常数时间)

An ArrayList in Java is a List that is backed by an array.

The get(index) method is a constant time, O(1), operation.

The code straight out of the Java library for ArrayList.get(index):

public E get(int index) {
    RangeCheck(index);
    return (E) elementData[index];
}

Basically, it just returns a value straight out of the backing array. (RangeCheck(index)) is also constant time)

陌伤浅笑 2024-08-27 03:08:37

它的实现是通过数组完成的,并且获取操作的时间复杂度为 O(1)。

javadoc 说:

大小、isEmpty、获取、设置、
iterator 和 listIterator 操作以常量运行
时间。加法操作在摊销常数时间内运行
也就是说,添加n个元素需要O(n)时间。所有其他操作
以线性时间运行(粗略地说)。相比之下,常数因子较低
到 LinkedList 实现。

It's implementation is done with an array and the get operation is O(1).

javadoc says:

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.

2024-08-27 03:08:37

正如每个人都已经指出的那样,读取操作的时间复杂度为常数 - O(1),但写入操作有可能耗尽后备数组、重新分配和复制中的空间 - 因此运行时间为 O(n) ,正如医生所说:

大小、isEmpty、get、set、迭代器、
和 listIterator 操作运行在
恒定时间。 添加操作运行
在摊余常数时间内,即
添加 n 个元素需要 O(n) 时间。

所有其他操作都运行在
线性时间(粗略地说)。这
与以下相比,常数因子较低
对于 LinkedList 来说
实施。

实际上,经过几次添加后,一切都是 O(1),因为每次其容量耗尽时,后备数组都会加倍。因此,如果数组从 16 开始,满了,它会被重新分配到 32,然后是 64、128,等等。所以它可以扩展,但 GC 可能会在大的重新分配期间发生。

As everyone has already pointed out, read operations are constant time - O(1) but write operations have the potential to run out of space in the backing array, re-allocation, and a copy - so that runs in O(n) time, as the doc says:

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.

In practice everything is O(1) after a few adds, since the backing array is doubled each time it's capacity is exhausted. So if the array starts at 16, gets full, it's reallocated to 32, then 64, 128, etc. so it scales okay, but GC can hit up during a big realloc.

§普罗旺斯的薰衣草 2024-08-27 03:08:37

迂腐地说,它是一个由数组支持的 List。是的,get() 的时间复杂度是 O(1)。

To be pedantic, it's a List backed by an array. And yes, the time complexity for get() is O(1).

岁月苍老的讽刺 2024-08-27 03:08:37

只是一个注释。

get(index) 方法的时间复杂度为常数,O(1)

但如果我们知道索引,情况就是如此。如果我们尝试使用 indexOf(something) 获取索引,这将花费 O(n)

Just a Note.

The get(index) method is a constant time, O(1)

But that's the case if we know the index. If we try to get the index using indexOf(something), that will cost O(n)

柠檬 2024-08-27 03:08:37

arraylist 基本上是数组的实现。所以对其进行 CRUD 操作的时间复杂度为:

get/read :  O(1) since you can seek the address directly from base

remove/delete : O(n) why ? Because once we delete the element at index, then we need to move the after values one by one to the left. 

add : O(1) becuase you always add the end of the array - next free address available.

update : O(1) since you are just changing the value but nothing value moving....across the array.

The arraylist is basically an implementation of array. so the time complexity of the CRUD operations on it would be :

get/read :  O(1) since you can seek the address directly from base

remove/delete : O(n) why ? Because once we delete the element at index, then we need to move the after values one by one to the left. 

add : O(1) becuase you always add the end of the array - next free address available.

update : O(1) since you are just changing the value but nothing value moving....across the array.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文