java使用栈

开始想直接new Stack<>(); 然后顺手点开了源码

Stack源码:

public class Stack<E> extends Vector<E> {
	//构造略
	public E push(E item);
	public synchronized E pop();
	public synchronized E peek();
	public boolean empty();
	public synchronized int search(Object o);
}

stack文档:

The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
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 stack = new ArrayDeque();

翻译成人话:

Stack类是一个后进先出(LIFO)堆栈。它用五个方法扩展了类Vector,使之成为堆栈。
提供了:push,pop,查看栈顶的方法,是否为空的方法,搜索项并发现其离顶部有多远的方法。

第一次声明为空

Deque接口及其实现提供了一组更完整、更一致的LIFO堆栈操作,应该优先使用此类。例如:
Deque stack = new ArrayDeque();

cao 一键白学
看Deque吧

Deque接口

双端队列deque

这玩意啥啥都能干呢。。。
当做堆栈用的时候,就仨方法有用

Stack Method	Equivalent Deque Method等效Deque法
push(e)		addFirst(e)
pop()		removeFirst()
peek()		getFirst()
posted @ 2022-03-31 20:26  荧惑微光  阅读(109)  评论(0)    收藏  举报