Stack使用攻略:
package com.neusoft.data.structure;
import java.util.*;
/**
* @author Administrator
*/
public class MyStack {
/**
* @param args
*/
public static void main(String[] args) {
Stack stack = new Stack();
System.out.println("11111, 叔叔, 29999.3 三个元素入栈");
stack.push(new Integer(11111));
stack.push(new Integer(22222));
stack.push("卫叔叔");
stack.push(new Double(29999.3));
stack.push(new Double(1212));
String s = new String("卫叔叔");
//3 由上到下 第一个位置索引为1
System.out.println("元素卫叔叔在堆栈的位置"+stack.search(s));
printStack(stack);
}
/**
* 遍历的顺序为什么不是他喵的后进先出?
* @param stack
*/
private static void printStack(Stack<Integer> stack ){
if (stack.empty()){
System.out.println("堆栈是空的,没有元素");
}else {
System.out.print("堆栈中的元素:");
Enumeration items = stack.elements();
while (items.hasMoreElements()){
System.out.print(items.nextElement()+" ");
}
}
System.out.println(); //换行
}
}
输出: