Questions tagged [arraydeque]

ArrayDeque is an object which implements an array-based double-ended queue data structure.

ArrayDeque has the following properties:

  • It is a generalization of a stack and a queue
  • Inserting and removing items from the front or back is possible in constant worst-case time

References

89 questions
252
votes
10 answers

Why is ArrayDeque better than LinkedList

I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface. I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it…
Cruel
  • 2,523
  • 2
  • 15
  • 4
37
votes
2 answers

ArrayDeque vs ArrayList to implement a stack

The documentation for ArrayDeque says: This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue. There is no mention of the difference between using an ArrayDeque as a stack and using an…
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
24
votes
6 answers

How is ArrayDeque faster than stack?

According to javadoc, ArrayDeque class is likely to be faster than Stack when used as a stack I don't understand how can ArrayDeque be faster than stack. Suppose stack is implemented using linkedlist as follows: Push: Insert new element at the…
Gaurav
  • 1,005
  • 3
  • 14
  • 33
10
votes
2 answers

Differnce between addfirst and offerFirst methods in ArrayDeque

Have tried out a sample program to understand the difference between addFirst and offerFirst methods in ArrayDeque of Java 6. But they seem to be same, any suggestions? public void interfaceDequetest() { try{ ArrayDeque ad =…
user288686
  • 135
  • 2
  • 11
9
votes
4 answers

Why is Deque (ArrayDeque) capacity a power of two?

In Java (but similarly in PHP) the ArrayDeque implementation always has its capacity as a power of 2: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/ArrayDeque.java#l126 For HashMap this choice is clear - to…
radistao
  • 14,889
  • 11
  • 66
  • 92
9
votes
3 answers

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…
Taylous
  • 260
  • 4
  • 15
9
votes
4 answers

Difference between add(E e) and offer(E e) of ArrayDqueue Class

Hi I used add and offer to add my element in last pace. Both are returning boolean and both does not throw any exception apart from NPE. public class ArrayDequeDemo { public static void main(String[] args) { // Create ArrayDeque elements. …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
8
votes
2 answers

Why the ArrayDeque class use bitwise operation in the pollFirst method?

I look through java source code try to learn the implementation of collection. Found a interesting thing in the ArrayDeque class. public E pollFirst() { int h = head; @SuppressWarnings("unchecked") E result = (E) elements[h]; //…
Albert Gao
  • 3,653
  • 6
  • 40
  • 69
7
votes
2 answers

addFirst method of ArrayDeque Class

The code of addFirst method in java.util.ArrayDeque class is public void addFirst(E e) { if (e == null) throw new NullPointerException(); elements[head = (head - 1) & (elements.length - 1)] = e; if (head == tail) …
Pankz
  • 83
  • 1
  • 5
6
votes
3 answers

About deque's extra indirection

Wondering why my memory accesses were somewhat slower than I expected, I finally figured out that the Visual C++ implementation of deque indeed has an extra layer of indirection built-in, destroying my memory locality. i.e. it seems to hold an array…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
2 answers

java: how to addAll(Collection<>) to the front of the queue?

public void traverse(Node root){ ArrayDeque queue = new ArrayDeque(); queue.add(root); while(!queue.isEmpty()){ Node currentNode = queue.pollFirst(); List nl = getChildrenfromDB(currentNode); …
KJW
  • 15,035
  • 47
  • 137
  • 243
6
votes
4 answers

Why typical Array List implementations aren't double-ended?

Why aren't ArrayLists generally implemented to be double-ended, which would support fast amortized insertion in the front as well as the back? Is there ever a disadvantage to using the latter over the former? (I'm not talking just about Java -- I…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
2 answers

Convert Array of Integers to ArrayDeque Java

How can I to convert Array of Integers to ArrayDeque? For example, instead to add numbers in ArrayDeque with loop, will I can to convert this Array of integers directly to ArrayDeque? Thanks in advance.
fire147
  • 63
  • 1
  • 3
  • 9
6
votes
4 answers

ArrayDeque add multiple elements

Im using arraydeque to create list of items and pass them parameters(Items is class) ArrayDeque Items= new ArrayDeque(); But I have problem with java ArrayDeque. Maybe there are ways to add more than one element at once. For example. I…
Martynas Žukovas
  • 149
  • 1
  • 5
  • 13
5
votes
2 answers

Assigning a size to a Deque in Java

I'm having trouble assigning a limit to the size of my double ended queue (Deque). It seems that my queue never gets full and just resizes whenever I add or offer a value unto it. My simple code just accepts a string value, splits it by space " ",…
Juni
  • 79
  • 2
  • 7
1
2 3 4 5 6