Stack类
序号 | 方法描述 |
---|---|
1 | boolean empty() 测试堆栈是否为空。 |
2 | Object peek( ) 查看堆栈顶部的对象,但不从堆栈中移除它。 |
3 | Object pop( ) 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
4 | Object push(Object element) 把项压入堆栈顶部。 |
5 | int search(Object element) 返回对象在堆栈中的位置,以 1 为基数。 |
基本使用:
private static void solution(int n) {
Stack<Integer> st = new Stack<Integer>();
for (int i = 1; i <= n; i++) {
st.push(i);
}
while(!st.isEmpty()){
System.out.println(st.pop());
}
}
当n=7时,输出为
7
6
5
4
3
2
1