简体   繁体   English

java列表处理时间

[英]java List processing time

This is from wikipedia: http://en.wikipedia.org/wiki/Arraylist under Performance . 这来自Wikipedia: Performance下的http://en.wikipedia.org/wiki/Arraylist

ArrayList: constant time for remove(), add() at end of array, linear time to add(), remove() at beginning. ArrayList:remove()的恒定时间,数组末尾的add(),add()的线性时间,开始的线性时间。

LinkedList: both of operations stated : constant time, indexing: linear. LinkedList:这两个操作都说明:恒定时间,索引:线性。


1)Why the difference in arraylist processing time between the two operations? 1)为什么两个操作之间的arraylist处理时间不同?

2)Linkedlist is linear for indexing, constant for adding at the end, why? 2)Linkedlist是线性的用于索引编制,常数用于最后的添加,为什么?

1) Because to add/remove at the beginning it has to shift everything and reindex. 1)因为要在开始时添加/删除,所以必须移动所有内容并重新索引。

2) Because it maintains references to the head and tail (beginning & end). 2)因为它维护对头和尾(开头和结尾)的引用。 Indexing means traversing the list. 索引表示遍历列表。

i) ArrayList -> You've got to push all the elements by one position in case of removal/addition in the beginning, hence linear time. i)ArrayList->如果要在开始处进行删除/添加操作,则必须将所有元素推到一个位置,因此是线性时间。 At the end of array, you simply add or remove. 在数组末尾,您只需添加或删除即可。

ii)LinkedList -> You have references of head and tail, hence you can add/remove anything there (in constant time). ii)LinkedList->您有头和尾的引用,因此您可以(在固定时间内)在其中添加/删除任何内容。

  1. Because removing at the end does not require moving the data. 因为最后删除不需要移动数据。 Adding may require copying to resize the storage array, but it's time is amortized . 添加可能需要复制以调整存储阵列的大小,但是时间已摊销
  2. Because adding at the end does not require walking the list, but indexing does. 因为在末尾添加不需要遍历列表,但索引却需要遍历。

When you add to the end of an ArrayList , it will grow itself to have some room to spare. 当您addArrayList的末尾时,它将自身增长以留出一些空间。 So if you have a ten-element ArrayList , adding at the end will cause it to internally allocate room for twenty elements, copy the ten you already had, and then add one. 因此,如果您有一个由十个元素组成的ArrayList ,则在末尾添加将导致它在内部为二十个元素分配空间,复制您已有的十个元素,然后添加一个。 Then, when you add another element at the end, it just sticks that twelfth element into the space it already created. 然后,当您在末尾添加另一个元素时,它只是将第十二个元素粘贴到已经创建的空间中。

This does not technically give it constant time insertion at the end, but it does give it amortized constant time insertion. 从技术上讲,这不会在最后插入固定时间,但确实会摊销固定时间。 That is to say, over a large number of operations, the cost approaches constant time; 也就是说,在大量操作中,成本接近固定时间。 each time it grows, it doubles, so you'll have an ever-larger number of "free" constant-time inserts before you have to grow-and-copy again. 每次增长,它都会加倍,因此在您不得不再次增长和复制之前,您将拥有越来越多的“免费”恒定时间插入。

When you insert at the beginning , it can't do this and must always copy the whole list into a new location (linear time). 当您在开头插入时,它不能执行此操作,必须始终将整个列表复制到新位置(线性时间)。

Removal from the end is always constant time because you just switch the last cell from being "filled" to "free space". 从最后删除总是恒定的时间,因为您只需将最后一个单元格从“填充”切换为“可用空间”即可。 You never need to copy the list. 您无需复制列表。

As for your second question, a LinkedList keeps a pointer to the end of the list, so add and remove there just use that pointer and are thus constant time. 至于第二个问题, LinkedList保持指向列表末尾的指针,因此仅在使用该指针的情况下在其中addremove指针即可,因此时间恒定。 There are no quick pointers into the middle of the list, so accessing an arbitrary element requires a linear-time traversal from start to (potentially) finish. 列表的中间没有快速指针,因此访问任意元素都需要从开始到(可能)结束的线性时间遍历。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM