简体   繁体   English

ArrayList VS ArrayDeque 在移动元素中?

[英]ArrayList VS ArrayDeque in shifting elements?

I tried asking a similar question, but I failed to receive any satisfying answer.我试着问一个类似的问题,但我没有得到任何令人满意的答案。 The motivation behind this question is this question 's first (accepted) answer that roughly says:这个问题背后的动机是这个问题的第一个(接受的)答案,大致说:

ArrayDeque doesn't have the overhead of shifting contents like ArrayList. ArrayDeque 没有像 ArrayList 那样移动内容的开销。

From my point of view, they should act the same.在我看来,他们应该采取同样的行动。 The only difference is that ArrayList is implemented from List interface, meaning it can access any arbitrary index .唯一的区别是ArrayList是从List接口实现的,这意味着它可以访问任何任意索引 On the other side, ArrayDeque is implemented from Queue interface and it works in LIFO/FIFO manner.另一方面, ArrayDeque是从Queue接口实现的,它以LIFO/FIFO方式工作。

The thing I want to point is that they both use AN ARRAY to store elements.我想指出的是,它们都使用AN ARRAY来存储元素。 Meaning if they both had an array with these elements:这意味着如果他们都有一个包含这些元素的数组:

2, 4, 6, 8, 10

, doing arraylist.remove(0); , 做arraylist.remove(0); and arraydeque.poll();arraydeque.poll(); should both remove first/head element with value 2.应该都删除值为 2 的第一个/头元素。

Now my big question.现在我的大问题。 Do all those left numbers (4,6,8,10) get shifted to left for 1 slot in both cases?在这两种情况下,所有左边的数字 (4,6,8,10) 都会向左移动 1 个插槽吗? Is there any difference between ArrayList and ArrayDeque in way how they shift elements when we do any structure modification?当我们进行任何结构修改时, ArrayListArrayDeque之间在移动元素的方式上有什么区别吗?

In ArrayList , each of the elements gets shifted over.ArrayList ,每个元素都被转移了。

In ArrayDeque , the elements don't move in the array.ArrayDeque ,元素不会在数组中移动 The pointer in the array that says where the current head of the queue is gets moved over.数组中的指针表示当前队列的头部被移动到哪里。

(Why doesn't ArrayList do this, you might ask? It could conceivably do it, but it'd only work for inserting/removing elements at the beginning and end, not in the middle. ArrayList isn't really designed to be used that way -- if you're doing that, then you have a queue/deque, so you might as well use ArrayDeque .) (你可能会问,为什么ArrayList不这样做?它可以做到,但它只适用于在开头和结尾插入/删除元素,而不是在中间ArrayList并不是真正设计用于使用的那样——如果你这样做,那么你有一个队列/双端队列,所以你不妨使用ArrayDeque 。)

ArrayDeque maintains a small internal array of size 16, and maintains the pointer to head and tail until its size reach its limit. ArrayDeque 维护一个大小为 16 的小型内部数组,并维护指向头和尾的指针,直到其大小达到极限。 Once it reach it limits, it will double the size of the array.一旦达到极限,数组的大小就会加倍。 All the operations, we do on ArrayDeque are managed with head and tail pointers in hand.我们在 ArrayDeque 上进行的所有操作都是通过头指针和尾指针进行管理的。

But in case of ArrayList, majority of operations are using index of array.但是在 ArrayList 的情况下,大多数操作都使用数组的索引。 And it starts from 0. So in case of ArrayList it has to move all elements of ArrayList if we remove first element from it to maintain its index.它从 0 开始。因此,在 ArrayList 的情况下,如果我们从中删除第一个元素以维护其索引,则必须移动 ArrayList 的所有元素。 That is not a challenge for ArrayDeque, it can just update the pointer of tail or head depends on from where we are removing the item.这对 ArrayDeque 来说不是挑战,它可以根据我们从何处删除项目来更新 tail 或 head 的指针。

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

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