Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayDeque is implemented as an array, why is it not Random Access?

I know that ArrayDeque is fast when adding and removing simple lists. I tested it, it was quicker to add and delete than LinkedList. Because I know that it is implemented as an Array, so why not Random Access?

I read the ArrayDeque.java file in the Java src. But I do not understand it well with my English level. I've seen a lot of articles from Google and Stack Overflow, but I did not get the answers I wanted.

In conclusion, what I'm looking for is:

  1. Why is ArrayDeque not Random Access? (I am most curious)
  2. In what situations is ArrayDeque used?
  3. Is ArrayDeque not implemented as an Array? (Did I misunderstand this?)

Thank you very much for your reply!

like image 692
Taylous Avatar asked Jan 24 '19 00:01

Taylous


1 Answers

The answer is that there's no good reason. It would be easy to add a constant-time get(int) and set(int,E) to ArrayDeque. More than once I've had to implement the algorithms of ArrayDeque within an ArrayList to make up for that lack.

like image 118
Matt Timmermans Avatar answered Oct 14 '22 09:10

Matt Timmermans