简体   繁体   English

当数组已满时,我们为数组重新分配内存时,可扩展数组会做什么?

[英]What does extendable array do when we re-allocate the memory for array when it's full?

I'm just encountered with this question that if we have a dynamically allocated array, it takes O(1) to do the insertion. 我只是遇到这个问题,如果我们有一个动态分配的数组,则需要O(1)进行插入。 But when the array is full, we need to re-allocate double space to the array, so copying of the old array takes O(n). 但是,当数组已满时,我们需要为数组重新分配双倍空间,因此复制旧数组需要O(n)。

Is there any way we can make it O(1)? 有什么办法可以使它成为O(1)?

I have read some articles saying about the extendable array but I don't quiet understand it. 我读过一些有关可扩展数组的文章,但是我并没有对此有所了解。 Can anyone help explain it more? 谁能帮忙解释一下?

Thanks a lot. 非常感谢。

Allocating double the space each time forms a geometric progression. 每次分配两倍的空间会形成几何级数。 This means that while some insertions (those that trigger the expansion) are very expensive, they are so infrequent that the amortized performance is still O(1). 这意味着,尽管某些插入(触发扩展的插入)非常昂贵,但它们很少出现,因此摊销后的性能仍然为O(1)。 For example, inserting a billion elements only requires 30 doublings (2^30=1,073,741,824). 例如,插入十亿个元素仅需要30倍(2 ^ 30 = 1,073,741,824)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们使用空花括号分配数组时,内存中究竟发生了什么? - What exactly happens in memory when we allocate an array using an empty curly braces? 读取时二进制1的数组是什么意思 - What does array of 1's mean in binary when reading 在java中创建数组时,new运算符会做什么? - What does the new operator do when creating an array in java? 当数组变量是volatile时,我们是否需要同步对数组的访问? - Do we need to synchronize access to an array when the array variable is volatile? JVM需要分配多少内存来分配一个字符数组? - Home much memory does the JVM need to allocate a character array? 当我们不知道 Java 中的实际大小时如何定义数组的长度 - how to define a array's length when we do not know the actural size in Java 当ActiveMQ的队列在生产环境中已满时该怎么办? - What to do when ActiveMQ's queue is full in productive environment? 当磁盘存储空间已满时,Glide 会做什么? - what does Glide do when disk storage is full? 在创建非文字大小的数组时,jvm如何分配memroies - how does jvm allocate memroies when creating array with non literal size 当我们执行 Modelservice.Save() 时,Hybris 做了什么? - What Hybris Does when we do Modelservice.Save()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM