Stack S=new Stack(10);
int c;
S.push(123);//将123压入栈
c=S.peek(); //取出栈顶元素,此时报错,不能将Object转为int
请问应该怎么解决?强制转换肯定不行,也不能将c声明为Object,因为以后还要用int型的c来进行运算
------解决方案--------------------
package test;
import java.util.Stack;
public class TestInteger {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
Stack s = new Stack();
s.push( "123 ");
i = Integer.parseInt(s.peek().toString());
System.out.print(i);
}
}
------解决方案--------------------
如果是JDK 5.0
int i;
Stack <Integer> s = new Stack <Integer> ();
s.push(123);
i = s.peek();