java vector search_【Java集合系列三】Vector-Stack解析

本文详细介绍了 Java 中 Stack 类的功能和使用方法,包括如何创建空栈、压栈、弹栈、查看栈顶元素等操作,并提供了典型示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 packagejava.util;2

3 /**

4 * The Stack class represents a last-in-first-out5 * (LIFO) stack of objects. It extends class Vector with five6 * operations that allow a vector to be treated as a stack. The usual7 * push and pop operations are provided, as well as a8 * method to peek at the top item on the stack, a method to test9 * for whether the stack is empty, and a method to search10 * the stack for an item and discover how far it is from the top.11 *

12 * When a stack is first created, it contains no items.13 *14 *

A more complete and consistent set of LIFO stack operations is15 * provided by the {@linkDeque} interface and its implementations, which16 * should be used in preference to this class. For example:17 *

   {@code

18 * Deque stack = new ArrayDeque();}

19 *20 *@authorJonathan Payne21 *@sinceJDK1.022 */

23 public

24 class Stack extends Vector{25 /**

26 * Creates an empty Stack.27 */

28 publicStack() {29 }30

31 /**

32 * Pushes an item onto the top of this stack. This has exactly33 * the same effect as:34 *

35 * addElement(item)
36 *37 *@paramitem the item to be pushed onto this stack.38 *@returnthe item argument.39 *@seejava.util.Vector#addElement40 */

41 publicE push(E item) {42 addElement(item);43

44 returnitem;45 }46

47 /**

48 * Removes the object at the top of this stack and returns that49 * object as the value of this function.50 *51 *@returnThe object at the top of this stack (the last item52 * of the Vector object).53 *@throwsEmptyStackException if this stack is empty.54 */

55 public synchronizedE pop() {56 E obj;57 int len =size();58

59 obj =peek();60 removeElementAt(len - 1);61

62 returnobj;63 }64

65 /**

66 * Looks at the object at the top of this stack without removing it67 * from the stack.68 *69 *@returnthe object at the top of this stack (the last item70 * of the Vector object).71 *@throwsEmptyStackException if this stack is empty.72 */

73 public synchronizedE peek() {74 int len =size();75

76 if (len == 0)77 throw newEmptyStackException();78 return elementAt(len - 1);79 }80

81 /**

82 * Tests if this stack is empty.83 *84 *@returntrue if and only if this stack contains85 * no items; false otherwise.86 */

87 public booleanempty() {88 return size() == 0;89 }90

91 /**

92 * Returns the 1-based position where an object is on this stack.93 * If the object o occurs as an item in this stack, this94 * method returns the distance from the top of the stack of the95 * occurrence nearest the top of the stack; the topmost item on the96 * stack is considered to be at distance 1. The equals97 * method is used to compare o to the98 * items in this stack.99 *100 *@paramo the desired object.101 *@returnthe 1-based position from the top of the stack where102 * the object is located; the return value -1103 * indicates that the object is not on the stack.104 */

105 public synchronized intsearch(Object o) {106 int i =lastIndexOf(o);107

108 if (i >= 0) {109 return size() -i;110 }111 return -1;112 }113

114 /**use serialVersionUID from JDK 1.0.2 for interoperability*/

115 private static final long serialVersionUID = 1224463164541339165L;116 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值