Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
33 lines (28 loc) · 1.09 KB

File metadata and controls

33 lines (28 loc) · 1.09 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package CollectionTest;
import java.util.Iterator;
import java.util.Stack;
/*
* Stack 矢量队列,继承于Vector ,是使用数组实现栈
* Stack<E> extends Vector<E>
* */
public class StackTest {
public static void main(String[] args) {
// List<Integer> stack=new Stack(); //向上转型
Stack<Integer> stack = new Stack();
for (int a = 1; a < 4; a++) {
stack.push(a);
}
// stack.push(1); 将元素推入栈顶
// stack.pop() 将一个元素从栈顶删除,实际上是调用Vectro的方法removeElementAt
// stack.peek() 返回栈顶元素,不执行删除操作
// stack.search() 搜索栈内元素,实际上是调用Vector的indexOf方法,返回数组的索引+1
//遍历方法:1.for循环 2.iterator遍历
for (Iterator iterator = stack.iterator(); iterator.hasNext(); ) {
System.out.println(iterator.next());
}
stack.pop();
for (Iterator iterator = stack.iterator(); iterator.hasNext(); ) {
System.out.print(iterator.next());
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.