【发布时间】:2019-11-29 15:12:43
【问题描述】:
当我了解堆栈和队列时,它在 ArrayList 上使用堆栈/队列。 但是,我通过 Intellij 搜索 API,Stack 和 Queue 在列表集合中使用 ArrayDeque 类,而不是 ArrayList。
/**
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code * Deque<Integer> stack = new ArrayDeque<Integer>();}
*/
在队列中,它使用 LinkedList API 中的 LinkedList 类。 还有大多数人的代码,例如:
Queue<Integer> q1 = new LinkedList<>()
/**
* Queue operations.
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
重点是,在解释堆栈和队列的概念时,请使用 ArrayList。
但是,实际上,使用 LinkedList 或 ArrayDeque,而不是 ArrayList。 你能解释一下为什么吗?
【问题讨论】:
标签: java data-structures linked-list stack queue