gpt4 book ai didi

java - Java中如何定义栈

转载 作者:行者123 更新时间:2023-12-01 10:17:17 25 4
gpt4 key购买 nike

我知道 Java 通过扩展 Vector 搞砸了堆栈实现。默认情况下 vector 是同步的。阅读java文档,它说要创建一个堆栈,

 Deque<Integer> stack = new ArrayDeque<Integer>();

因为我对java比较陌生。我的问题是:这是在 Java 中定义堆栈的最佳实践吗?

最佳答案

阅读Stack的javadoc :

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:

Deque<Integer> stack = new ArrayDeque<Integer>();

甚至 Deque 的 javadoc 也这么说:

Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class.

遵循 java 文档的建议是“最佳实践”吗?
是的。

关于java - Java中如何定义栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35822194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com