admin管理员组文章数量:1655505
Demonstrate 演示 generic 通用、泛型
import java.util.EmptyStackException;
import java.util.Stack;
public class StackDemo {
static void showpush(Stack<Integer> st,int a){
st.push(a);
System.out.println("push(" + a +")");
System.out.println("Stack:" + st );
}
static void showpop(Stack<Integer> st){
System.out.println("pop -> ");
Integer a = st.pop();
System.out.println(a);
System.out.println("Stack:" + st);
}
public static void main(String[] args) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("Stack:" + st);
showpush(st,42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
/**
* Stack:[]
* push(42)
* Stack:[42]
* push(66)
* Stack:[42, 66]
* push(99)
* Stack:[42, 66, 99]
* pop ->
* 99
* Stack:[42, 66]
* pop ->
* 66
* Stack:[42]
* pop ->
* 42
* Stack:[]
* pop ->
* empty stack
*/
}
}
本文标签: demonstrateclassStackgeneric
版权声明:本文标题:9. 13. 7. Demonstrate the generic Stack class. 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729699629a1210451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论