Java 实现堆栈';s在静态数组中的push方法,但如果push的数量大于;初始数组大小
这是我第一次作为大学作业的一部分学习Java,在大学作业中我必须实现堆栈数据结构,但挑战是使代码在大于初始数组大小的情况下工作。我不允许在这项任务中使用ArrayList。我只能使用静态数组,并在需要时通过将内容复制到新数组并将旧数组重新指定为新数组来调整大小 我尝试使用if条件来实现这一点,该条件确定计数器(最初为0,随着推送次数的增加而增加)是否大于最初定义的数组大小 然后,我创建了一个名为stackextender的新方法,在该方法中,我将超过的值放入一个名为extensionstack的新数组中。然后在方法mystackmodifier上,我创建了一个temparray,并将原始堆栈和extensionstack的内容放入temparray中。最后,我使用mystack=temparray将内容复制到原始堆栈中。我认为代码在这个函数(mystackmodifier)中有问题,我正在修改初始堆栈的内容。代码发布如下:Java 实现堆栈';s在静态数组中的push方法,但如果push的数量大于;初始数组大小,java,arrays,data-structures,stack,Java,Arrays,Data Structures,Stack,这是我第一次作为大学作业的一部分学习Java,在大学作业中我必须实现堆栈数据结构,但挑战是使代码在大于初始数组大小的情况下工作。我不允许在这项任务中使用ArrayList。我只能使用静态数组,并在需要时通过将内容复制到新数组并将旧数组重新指定为新数组来调整大小 我尝试使用if条件来实现这一点,该条件确定计数器(最初为0,随着推送次数的增加而增加)是否大于最初定义的数组大小 然后,我创建了一个名为stackextender的新方法,在该方法中,我将超过的值放入一个名为extensionstack的
public class StackOperations{
int mystack[];
int global_size;
int counter = 0;
int newcounter = 0;
int extensionstack[] = new int[counter*2];
public StackOperations(int size){
this.mystack = new int[size]; //create a stack of size as mentioned while creating object
this.global_size = size; //update the global variable
}
//the below method will allow us to calculate the number of
public void push(int value){
if (counter<global_size){
mystack[counter] = value;
counter += 1;//we have to do this unless counter < stacksize
}
else{
/*so if I have to make my code work for user input greater than my initial stack size,
I am creating a new method called stackextender where I put new value in a new array called
extension stack. Then on the method, mystackmodifier I create a temparray and put contents of
original stack and extensionstack in the temparray. At last i do mystack = temparray to
copy the contents to my original stack*/
stackextender(value);
counter ++;
}
}
//This method puts exceeding values to a new stack called extension stack
public void stackextender(int value){
extensionstack[newcounter] = value;
newcounter ++;
}
//This method modifies the contents of original stack
public void mystackmodifier(){
int temparray[] = new int[(global_size+newcounter)*2];
if(extensionstack.length>0){
int key;
for ( key = 0; key<mystack.length; key++){
temparray[key] = mystack[key];
}
for (int i = 0; i<newcounter; i++){
temparray[key+i] = extensionstack[i];
}
mystack = temparray;
}
}
公共类堆栈操作{
int mystack[];
int全局_大小;
int计数器=0;
int newcounter=0;
int extensionstack[]=新int[计数器*2];
公共堆栈操作(整数大小){
this.mystack=new int[size];//在创建对象时创建一个所述大小的堆栈
this.global_size=size;//更新全局变量
}
//下面的方法将允许我们计算
公共无效推送(int值){
如果(计数器0){
int键;
对于(key=0;keyWell,一个问题是extensionstack
被初始化为一个空数组。是的,这就是我想要做的。我想把超推的内容放入extensionstack
中,我试图在mystackmodifier
方法中组合成一个新数组-temparray。然后我将mystack
重新分配给temparray
好吧,在这里将该数组初始化为0长度-int extensionstack[]=new int[counter*2];
,因为此时计数器为0。您应该在构造函数中对其进行初始化。好的,我这样做了。我还有一个toString方法和所有其他我没有在这里发布的方法。现在我在toString方法中得到IndexAutofBound异常。您能看看吗。为什么需要一个单独的stackextender类?为什么不扩展arr需要时,是否在push()的内部?
import java.util.*;
public class ArrayStack{
public static void main(String args[]){
StackOperations mystack = new StackOperations(100);
System.out.println(mystack.toString());
/*Although the stack size is instantiated as 10, the code works for insertion for as many
number of items as demonstrated below*/
for (int i = 0; i<150; i+=1){
mystack.push(i);
}
int stacksize = mystack.getSize();
if (stacksize == 0) {
try{
throw new NoSuchElementException();
}catch (NoSuchElementException exc){
exc.printStackTrace();
}
}
System.out.println(mystack.toString());
}