java程序报错,求修改。 StackOfIntegers stack=new StackOfIntegers();和 this(DEFAULT_CAPACITY);报错
publicclassA02{publicstaticvoidmain(String[]args){StackOfIntegersstack=newStackOfInte...
public class A02 {
public static void main(String[] args) {
StackOfIntegers stack=new StackOfIntegers();
for(int i=0;i<10;i++)
stack.push(i);
while (!stack.empty())
System.out.print(stack.pop()+" ");
// TODO Auto-generated method stub
}
class StackOfIntegers{
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY=16;
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}
public void stackOfIntegers(int capacity){
elements=new int[capacity];
}
public void push(int value)
{
if(size>=elements.length)
{
int[] temp=new int[elements.length*2];
System.arraycopy(elements,0,temp,0,elements.length);
elements=temp;
}
elements[size++]=value;
}
public int pop(){
return elements[--size];
}
public int peek(){
return elements[size-1];
}
public boolean empty(){
return size==0;
}
public int getSize(){
return size;
}
}
} 展开
public static void main(String[] args) {
StackOfIntegers stack=new StackOfIntegers();
for(int i=0;i<10;i++)
stack.push(i);
while (!stack.empty())
System.out.print(stack.pop()+" ");
// TODO Auto-generated method stub
}
class StackOfIntegers{
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY=16;
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}
public void stackOfIntegers(int capacity){
elements=new int[capacity];
}
public void push(int value)
{
if(size>=elements.length)
{
int[] temp=new int[elements.length*2];
System.arraycopy(elements,0,temp,0,elements.length);
elements=temp;
}
elements[size++]=value;
}
public int pop(){
return elements[--size];
}
public int peek(){
return elements[size-1];
}
public boolean empty(){
return size==0;
}
public int getSize(){
return size;
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询