Java 中 C++ 的等价物是什么?双端队列?

发布于 2024-11-26 08:04:19 字数 313 浏览 2 评论 0原文

在 C++ 中我所要做的就是

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

但在 java 中我发现使用简单的双端队列非常困难 我应该怎么办...? 更具体地说, 我应该如何编码来简单地执行相同的步骤 就像我在 C++ 中所做的那样;包括、定义、使用。

更具体地说, 我想做一个双端队列,这样我就可以 在双端队列的前面或后面添加任何整数。 并按双端队列的大小打印该双端队列中的整数

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾北清歌寒 2024-12-03 08:04:19

当前的答案表明Java的 java.util.LinkedList< /a> 是 C++ 的 std::deque 的 Java 翻译。虽然 LinkedList 确实有一个大致相当于 std::deque 的接口,但它没有提供 std::deque 的复杂性保证> 确实如此。特别是,std::deque 保证通过索引进行 O(1) 查找(随机访问),而 LinkedList 具有 O(n ) 查找。从这个意义上(经验丰富的 C++ 用户查看 std::deque 的意义上来说),Java 的 LinkedListstd::deque 完全不同> (尽管它非常类似于 std::list)。 此线程为“什么是 Java 等价物”这个问题提供了更好的答案C++ 双端队列”。总而言之,标准 Java 库中没有等效的库。

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.

泪冰清 2024-12-03 08:04:19

Java 同时具有 QueueDeque 类型,以及一个 LinkedList 等可以充当以下任一者:

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

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);
友欢 2024-12-03 08:04:19

查看 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文