我在命令提示符下使用-Xlint来解决基于Stack的程序,但我的java程序正在收到警告。 我的代码是 StackDemo.java
import java.util.*;
public class StackDemo {
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack st) {
System.out.println("pop ->");
Integer a = (Integer)st.pop();
System.out.println(a);
System.out.println("stack:" + st);
}
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack:" + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
}
catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
答案 0 :(得分:0)
将Stack
更改为Stack<Integer>
import java.util.*;
public class StackDemo {
static void showpush(Stack<Integer> st, int a) {
st.push(new Integer(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);
showpop(st);
try {
showpop(st);
}
catch(EmptyStackException e) {
System.out.println("empty stack");
}
}
}