开发者

What is the Java equivalent of C++ deque?

in C++ all I had to do was

#include <queue> -> including
queue<int> a; -> defining
a.push(1); ->using

but in java I found very difficult to use 开发者_开发问答simple deque what should I do...? more specifically, How should I code to simply do the same steps as I did in C++; including, defining, using.

even more specifically, I want to make a deque so that I can add any integer in the deque at front or back. and print whole numbers in that deque by the size of the deque


The current answers suggest that Java's java.util.LinkedList is the Java translation of C++'s std::deque. While LinkedList does have an interface that is roughly equivalent to that of std::deque, it does not provide the complexity guarantees that std::deque does. In particular, std::deque guarantees O(1) lookup by index (random access), while LinkedList has O(n) lookup. In this sense (the sense in which an experienced C++ user views std::deque), Java's LinkedList is nothing at all like std::deque (though it is very much like std::list). This thread provides a better answer to the question "What is is the Java equivalent of C++ deque". To sum up, there is no equivalent in the standard Java library.


Java has both Queue and Deque types, and a LinkedList, among others, can act as either one:

import java.util.*;
Deque<Integer> q = new LinkedList<Integer>();
q.push(1);


Look at java.util.LinkedList.

LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(5);
linkedList.addFirst(2); // add to front, equivalent to push()
linkedList.addLast(3); // add to end, equivalent to add()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜