【问题标题】:Array Overflow Check数组溢出检查
【发布时间】:2020-11-14 23:55:17
【问题描述】:

我正在尝试检查数组是否溢出。 这是我正在使用的逻辑:

/*
        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");
    }
}

但是当我在我的数组上运行它时,我不断收到“堆栈未满。这是我的数组:

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);

我应该得到“堆栈溢出”的输出,但我不断得到“堆栈未满”。

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

全班

    int size; //initialize size
    int stackArray[]; //initialize array
    int top; //initialize top
    int stackArraySize;
    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 pushedElement
            System.out.println("The pushed element is: " + pushedElement);
        }
        else //if the stack is full, tell the user.
        {
            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]);
            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;
        }
    }

【问题讨论】:

  • 您的意思是检查
  • 如果我使用模数而不是除法会不会是这样?
  • 是的。我将 StackArraySize 设置为 temp。
  • 您的 Data 类是如何实现的,为什么要传递数组的大小来检查溢出。数组的大小将始终相同。
  • 我将 Data 类用作对象中心,在其中创建对象以在其上运行其他类。我正在传递数组的大小以检查我放入 stackArray 的元素 # 是否大于该大小。

标签: java arrays stack


【解决方案1】:

使用比率进行检查需要它是双精度数,否则类型转换会将其保持为默认整数。

话虽如此,您的push 方法目前不允许在定义长度后推送元素,您需要以下overflowCheck 才能工作。

class StackCheck {
    Data stackTest = new Data(10, false); //create a new stack & confirm it's not a queue
    public static void main() {
    
        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();
    }
    public void overflowCheck() {   
        double ratio = stackArray.length/stackTest.size;
        if(ratio > 1){
            System.out.println("stack is overflowing");
        }
        else if(ratio == 1){
           System.out.println("stack is full");
        }
        else{
           System.out.println("stack is not full");
        }
   
    }
 }

【讨论】:

    【解决方案2】:

    你必须修改你的逻辑并在 push 和 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);
        }
    
    }
    

    【讨论】:

    • 我将 stackArraySize 设置为 temp,然后将 stackArraySize 设置为 stackArray.length,然后运行该方法将 stackArraySize 与 temp 进行比较。现在我不断收到“堆栈溢出”
    • 根据您的数据集 stackSize 为 12,因此您传递为 12 .so temp=12 如果 arraysizetemp 你现在可以添加的下溢你决定在我的代码中哪个变量从数组中取出大小数组的大小是 stackArraySize
    • 请粘贴整个课程,否则会做出假设,如果它运作良好且良好,可能对您没有帮助
    • 编辑了你需要在 push 和 pop 中更新 stackArraySize 值的帖子
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多