Java 中的后进后出

发布于 2024-12-04 10:53:13 字数 191 浏览 2 评论 0原文

我有 90 个 ID,我需要如下图所示的内容。我希望首先弹出最后一个 ID,如果有新的 ID 添加到堆栈中,我想将它们推到堆栈的末尾。后进后出。这样的东西已经存在了吗?我知道我可以使用其他集合实现,但我想知道是否已经有这样的堆栈。

在此处输入图像描述

I have 90 IDs that I need to something like on the image below. I want the last ID to be popped first and if there are new IDs added to the stack I want to push them on the end of it. Last In Last Out. Does something like this exists already? I know I could use other collection implementations but I wonder if there is a stack like this already made.

enter image description here

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

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

发布评论

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

评论(6

成熟的代价 2024-12-11 10:53:13

Queue 是一个具有多种实现的接口(包括例如适合多线程解决方案的阻塞队列)

Queue is an interface with multiple implementations (including such things as blocking queues suitable for multi-threaded solutions)

自由范儿 2024-12-11 10:53:13

You probably want to have a FIFO (first-in-first-out) queue.

First have a look at Javadoc from java.util.Queue.

There exist several implementations:

暮色兮凉城 2024-12-11 10:53:13

您可以使用 Queue

You could use a Queue<E>.

孤寂小茶 2024-12-11 10:53:13

看起来像一个普通的队列实现,元素以相反的顺序添加到队列中。

looks like a normal queue implementation, with the elements added to the queue in reverse order to start off with.

小…楫夜泊 2024-12-11 10:53:13

您可以使用 java 提供的某种 Queue 实现(请参阅 队列 实现)。

另一种可能的选择是使用 LinkedList (参见:http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html

它提供了您需要的所有方法。特别是因为如果您不完全确定自己想要的行为,那么您的描述似乎就是如此。

至少对于不需要随机访问的大型集合来说,Queue 应该优先于 LinkedList

You may use sort of a Queue<E> implementation which is provided by java (see queue implementations).

Another possible Option would be to use a LinkedList<E> (see.: http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html)

It offers all methods you need. Especially because your description seems so if you are not totally sure about the behavior you want.

A Queue<E> should be preferred over a LinkedList<E> at least for large collections without the need of random access.

花伊自在美 2024-12-11 10:53:13

下面是一些可以帮助您入门的代码:

private static BlockingQueue<String> queue = new LinkedBlockingQueue<String>();

public static void main(String args[]) throws InterruptedException {
    // Start a thread that puts stuff on the queue
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    queue.put("Some message");
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    // Handle interruption
                }
            }
        }
    }).start();

    // Start a thread that takes stuff from the queue (LILO)
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    String message = queue.take(); // Waits if necessary for something to arrive
                    // Do something with message
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    // Handle interruption
                }
            }
        }
    }).start();

    Thread.currentThread().wait();
}

Here's some code to get you started:

private static BlockingQueue<String> queue = new LinkedBlockingQueue<String>();

public static void main(String args[]) throws InterruptedException {
    // Start a thread that puts stuff on the queue
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    queue.put("Some message");
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    // Handle interruption
                }
            }
        }
    }).start();

    // Start a thread that takes stuff from the queue (LILO)
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    String message = queue.take(); // Waits if necessary for something to arrive
                    // Do something with message
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    // Handle interruption
                }
            }
        }
    }).start();

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