ArrayList 文档的以下部分对我来说似乎不正确:
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.
现在集合定义为:
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
因此这可用于在 ArrayList 的中间添加一个元素并导致其余元素移动。
但这被认为是线性操作而不是恒定的。
我错了吗?还是我误解了什么?
最佳答案
这是一个集合操作,而不是一个添加。它只是替换数组的第 i 个条目。
关于java - 不清楚 ArrayList 的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8892098/