有两个错误 public StackOfInteger(int capacity)
{
element=new int[capacity];
// int[] element=new int[capacity]; //定义了局部变量,把字段隐藏了
}
public static void main(String[] args) {
StackOfInteger stack=new StackOfInteger();
for (int i=0; i<10; i++)
stack.push(i);//入栈10次
for (int i=0; i<stack.getElement().length; i++)//stack.getElement().length=16
System.out.println(stack.pop());//出栈16次,会有ArrayIndexOutOfBoundsException
}
{
element=new int[capacity];
// int[] element=new int[capacity]; //定义了局部变量,把字段隐藏了
}
public static void main(String[] args) {
StackOfInteger stack=new StackOfInteger();
for (int i=0; i<10; i++)
stack.push(i);//入栈10次
for (int i=0; i<stack.getElement().length; i++)//stack.getElement().length=16
System.out.println(stack.pop());//出栈16次,会有ArrayIndexOutOfBoundsException
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货