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 @   荧惑微光  阅读(91)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示