Java 数组溢出检查

Java 数组溢出检查,java,arrays,stack,Java,Arrays,Stack,我试图检查数组是否溢出。 这就是我使用的逻辑: /* store the length of the stackArray[] into a temp. int. compare the size of the temp int to the stackArray[] via 3 tests. If the stackArray[]'s size is greater than the int's size,

我试图检查数组是否溢出。 这就是我使用的逻辑:

/*
        store the length of the stackArray[] into a temp. int.
        compare the size of the temp int to the stackArray[] via 3 tests.
        
        If the stackArray[]'s size is greater than the int's size,
            then say that the array is overflowing, and that it doesn't have any more space to enter elements into.
        If the stackArray[]'s size is less than the int's size,
            then say that the stackArray[] is underflowing and that it still has space to enter some elements into it.
        If the stackArray[]'s size is equal to the int's size,
            then say that the array is full.
*/
public void overflowCheck(int temp)
{
    if(stackArraySize/stackArray.length > 100)
    {
        System.out.println("stack is overflowing");
    }
    else if(stackArraySize/stackArray.length < 100)
    {
       System.out.println("stack is not full");
    }
    else
    {
       System.out.println("stack is full");
    }
}
为了实现这一逻辑,我使用以下方法:

/*
        store the length of the stackArray[] into a temp. int.
        compare the size of the temp int to the stackArray[] via 3 tests.
        
        If the stackArray[]'s size is greater than the int's size,
            then say that the array is overflowing, and that it doesn't have any more space to enter elements into.
        If the stackArray[]'s size is less than the int's size,
            then say that the stackArray[] is underflowing and that it still has space to enter some elements into it.
        If the stackArray[]'s size is equal to the int's size,
            then say that the array is full.
*/
public void overflowCheck(int temp)
{
    if(stackArraySize/stackArray.length > 100)
    {
        System.out.println("stack is overflowing");
    }
    else if(stackArraySize/stackArray.length < 100)
    {
       System.out.println("stack is not full");
    }
    else
    {
       System.out.println("stack is full");
    }
}
我应该得到“堆栈溢出”的输出,但我一直得到“堆栈未满”

这是因为我的逻辑有缺陷还是我的执行有缺陷? 我该如何解决这个问题

全班


您必须修改您的逻辑并在push and pop方法中更新stackArraySize的值—您不需要在任何地方更新此值

public class Data {

    int size; // initialize size
    int stackArray[]; // initialize array
    int top; // initialize top
    int stackArraySize;
    int len;
    int[] Queue;
    int front;
    int rear;

    public Data(int size, boolean isArrayQueue) // constructor
    {
        if (isArrayQueue) {
            len = 0;
            Queue = new int[size];
            front = -1;
            rear = -1;
        } else {
            this.size = size;
            this.stackArray = new int[size];
            this.top = -1;
        }
    }

    public boolean isFull() // check if it's full
    {
        return (size - 1 == top);
    }

    public boolean isEmpty() // check if it's empty
    {
        return (top == -1);
    }

    public void push(int pushedElement) // push an element into the stack, as
                                        // long as isFull is false
    {
        
        
        if (!isFull()) // check if the stack is already full
        {
            top++; // increment top
            stackArray[top] = pushedElement; // set array[top] to the
            stackArraySize=top;                 // pushedElement
            System.out.println("The pushed element is: " + pushedElement);
        } else // if the stack is full, tell the user.
        {
            stackArraySize++;
            System.out.println("The stack is full.");
        }
    }

    public int pop() // pop an element from the stack, as long as isEmpty is
                        // false
    {
        if (!isEmpty()) // check is the stack is already empty.
        {
            int originalTop = top; // store original top value into originalTop
            top--;// decrement top
            System.out.println("The popped element is: " + stackArray[originalTop]);
            stackArraySize=top; 
            return stackArray[originalTop]; // return the originalTop again
        } else // if the stack is empty, tell the user.
        {
            System.out.println("The stack is empty.");
            return -1;
        }
    }

    public int top() // peek into the stack & return the element on the top
    {
        if (!this.isEmpty()) // if the stack has values, return the top element.
        {
            return stackArray[top];
        } else // if the stack is empty, tell the user.
        {
            System.out.println("The stack is empty");
            return -1;
        }
    }

    public void overflowCheck(int temp) {
        if (stackArraySize>= temp) {
            System.out.println("stack is overflowing");
        } else if (stackArraySize<temp) {
            System.out.println("stack is not full");
        } else {
            System.out.println("stack is full");
        }
    }

    public static void main(String args[]) {
        Data stackTest = new Data(10, false); // create a new stack & confirm
                                                // it's not a queue
        int stackSize = stackTest.size;
        stackTest.push(10); // push 10 into the stack
        stackTest.push(20); // push 20 into the stack
        stackTest.push(30); // push 30 into the stack
        stackTest.push(40); // push 40 into the stack
        stackTest.push(50); // push 50 into the stack
        stackTest.push(60); // push 60 into the stack
        stackTest.push(70); // push 70 into the stack
        stackTest.push(80); // push 80 into the stack
        stackTest.push(90); // push 90 into the stack
        stackTest.push(100); // push 100 into the stack
        stackTest.push(110); // push 110 into the stack
        stackTest.push(120); // push 120 into the stack

        stackTest.overflowCheck(stackSize);
        
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop();
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.overflowCheck(stackSize);
    }

}
公共类数据{
int size;//初始化大小
int stackArray[];//初始化数组
int top;//初始化top
int-stackArraySize;
内伦;
int[]队列;
内锋;
内部后部;
公共数据(整数大小,布尔isArrayQueue)//构造函数
{
if(isArrayQueue){
len=0;
队列=新整数[大小];
正面=-1;
后部=-1;
}否则{
这个。大小=大小;
this.stackArray=newint[size];
this.top=-1;
}
}
public boolean isFull()//检查是否已满
{
返回(大小-1==顶部);
}
public boolean isEmpty()//检查它是否为空
{
返回(顶部==-1);
}
public void push(int pushedElement)//将元素推入堆栈,如下所示
//只要isFull是假的
{
if(!isFull())//检查堆栈是否已满
{
top++;//递增top
stackArray[top]=pushedElement;//将数组[top]设置为
stackArraySize=top;//pushedElement
System.out.println(“推送的元素是:“+pushedeElement”);
}否则//如果堆栈已满,请告诉用户。
{
stackArraySize++;
System.out.println(“堆栈已满”);
}
}
public int pop()//从堆栈中弹出一个元素,只要isEmpty是空的
//假的
{
如果(!isEmpty())//检查堆栈是否已为空。
{
int originalTop=top;//将原始的top值存储到originalTop中
top--;//递减top
System.out.println(“弹出的元素是:“+stackArray[originalTop]);
stackArraySize=顶部;
return stackArray[originalTop];//再次返回originalTop
}else//如果堆栈为空,请告诉用户。
{
System.out.println(“堆栈为空”);
返回-1;
}
}
public int top()//查看堆栈并返回顶部的元素
{
if(!this.isEmpty())//如果堆栈有值,则返回top元素。
{
返回stackArray[top];
}else//如果堆栈为空,请告诉用户。
{
System.out.println(“堆栈为空”);
返回-1;
}
}
公共无效溢出检查(int temp){
如果(stackArraySize>=临时){
System.out.println(“堆栈溢出”);

}否则,如果(stackArraySize您必须修改您的逻辑并在push and pop方法中更新stackArraySize的值,则您不会在任何地方更新此值

public class Data {

    int size; // initialize size
    int stackArray[]; // initialize array
    int top; // initialize top
    int stackArraySize;
    int len;
    int[] Queue;
    int front;
    int rear;

    public Data(int size, boolean isArrayQueue) // constructor
    {
        if (isArrayQueue) {
            len = 0;
            Queue = new int[size];
            front = -1;
            rear = -1;
        } else {
            this.size = size;
            this.stackArray = new int[size];
            this.top = -1;
        }
    }

    public boolean isFull() // check if it's full
    {
        return (size - 1 == top);
    }

    public boolean isEmpty() // check if it's empty
    {
        return (top == -1);
    }

    public void push(int pushedElement) // push an element into the stack, as
                                        // long as isFull is false
    {
        
        
        if (!isFull()) // check if the stack is already full
        {
            top++; // increment top
            stackArray[top] = pushedElement; // set array[top] to the
            stackArraySize=top;                 // pushedElement
            System.out.println("The pushed element is: " + pushedElement);
        } else // if the stack is full, tell the user.
        {
            stackArraySize++;
            System.out.println("The stack is full.");
        }
    }

    public int pop() // pop an element from the stack, as long as isEmpty is
                        // false
    {
        if (!isEmpty()) // check is the stack is already empty.
        {
            int originalTop = top; // store original top value into originalTop
            top--;// decrement top
            System.out.println("The popped element is: " + stackArray[originalTop]);
            stackArraySize=top; 
            return stackArray[originalTop]; // return the originalTop again
        } else // if the stack is empty, tell the user.
        {
            System.out.println("The stack is empty.");
            return -1;
        }
    }

    public int top() // peek into the stack & return the element on the top
    {
        if (!this.isEmpty()) // if the stack has values, return the top element.
        {
            return stackArray[top];
        } else // if the stack is empty, tell the user.
        {
            System.out.println("The stack is empty");
            return -1;
        }
    }

    public void overflowCheck(int temp) {
        if (stackArraySize>= temp) {
            System.out.println("stack is overflowing");
        } else if (stackArraySize<temp) {
            System.out.println("stack is not full");
        } else {
            System.out.println("stack is full");
        }
    }

    public static void main(String args[]) {
        Data stackTest = new Data(10, false); // create a new stack & confirm
                                                // it's not a queue
        int stackSize = stackTest.size;
        stackTest.push(10); // push 10 into the stack
        stackTest.push(20); // push 20 into the stack
        stackTest.push(30); // push 30 into the stack
        stackTest.push(40); // push 40 into the stack
        stackTest.push(50); // push 50 into the stack
        stackTest.push(60); // push 60 into the stack
        stackTest.push(70); // push 70 into the stack
        stackTest.push(80); // push 80 into the stack
        stackTest.push(90); // push 90 into the stack
        stackTest.push(100); // push 100 into the stack
        stackTest.push(110); // push 110 into the stack
        stackTest.push(120); // push 120 into the stack

        stackTest.overflowCheck(stackSize);
        
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.pop();
        stackTest.pop(); 
        stackTest.pop(); 
        stackTest.overflowCheck(stackSize);
    }

}
公共类数据{
int size;//初始化大小
int stackArray[];//初始化数组
int top;//初始化top
int-stackArraySize;
内伦;
int[]队列;
内锋;
内部后部;
公共数据(整数大小,布尔isArrayQueue)//构造函数
{
if(isArrayQueue){
len=0;
队列=新整数[大小];
正面=-1;
后部=-1;
}否则{
这个。大小=大小;
this.stackArray=newint[size];
this.top=-1;
}
}
public boolean isFull()//检查是否已满
{
返回(大小-1==顶部);
}
public boolean isEmpty()//检查它是否为空
{
返回(顶部==-1);
}
public void push(int pushedElement)//将元素推入堆栈,如下所示
//只要isFull是假的
{
if(!isFull())//检查堆栈是否已满
{
top++;//递增top
stackArray[top]=pushedElement;//将数组[top]设置为
stackArraySize=top;//pushedElement
System.out.println(“推送的元素是:“+pushedeElement”);
}否则//如果堆栈已满,请告诉用户。
{
stackArraySize++;
System.out.println(“堆栈已满”);
}
}
public int pop()//从堆栈中弹出一个元素,只要isEmpty是空的
//假的
{
如果(!isEmpty())//检查堆栈是否已为空。
{
int originalTop=top;//将原始的top值存储到originalTop中
top--;//递减top
System.out.println(“弹出的元素是:“+stackArray[originalTop]);
stackArraySize=顶部;
return stackArray[originalTop];//再次返回originalTop
}else//如果堆栈为空,请告诉用户。
{
System.out.println(“堆栈为空”);
返回-1;
}
}
public int top()//查看堆栈并返回顶部的元素
{
if(!this.isEmpty())//如果堆栈有值,则返回top元素。
{
返回stackArray[top];
}else//如果堆栈为空,请告诉用户。
{
System.out.println(“堆栈为空”);
返回-1;
}
}
公共无效溢出检查(int temp){
如果(stackArraySize>=临时){
System.out.println(“堆栈溢出”);

}else if(stackArraySize使用比率检查需要它是双精度的,否则typecast将保留它作为默认整数

话虽如此,您的
推送
满足了要求