在自定义INT堆栈阵列中接收错误的结果



我正在为ints制作自己的堆栈数组。它可以正常工作,除了在stackarray添加一个值时,总会有一个0可以添加。每当我想收到窥视的值时,结果是它是空的。我大约更改了代码,但是当我将其更改为与给定的东西不同时,我会在执行过程中遇到错误

这是Stackarray类

public class StackArray{
//declare variables
int top, size, length;
int array[];
//constructor to initialize variables
public StackArray(int _size)
{
    length = 0;
    top = -1;
    size = _size;
    array = new int[size];
}
//push method to add numbers to the stack
void push(int newNum)
{
    //if statement to check if the stack is full
    if(top != size)
    {
        //update top and length
        top++;
        length++;
        array[top] = newNum;
    }
}
//method to remove the top number in the stack
int pop()
{
    //declare local variable
    int temp;
    //if statement to check if stack is not empty
    if(!isEmpty())
    {
        temp = top;
        top--;
        length--;
    }
    else
        System.out.println("No more items in Stack.");
    return top;
}
//boolean method to check if the stack is empty
boolean isEmpty()
{
    if(top == -1)
        return true;
    else
        return false;
}
//method to return the size of the stack
int size()
{
    return length;
}
//method to print out the top number in the stack
void peek() {
    if (isEmpty())
        System.out.println(array[top]);
    else
        System.out.println("Stack is empty");
}
//method to turn the stack into a String
public String toString()
{
    System.out.print("Stack: [");
    for(int i = 0; i <= length; i++)
    {
        System.out.print(array[i] + ", ");
    }
    System.out.println("]");
    return "";
}}

这是我用来运行程序的驱动程序类

import java.util.Scanner;
public class Driver
{
    public static void main(String[] args)
    {
        //declare variables and initialize scanner
        Scanner key = new Scanner(System.in);
        int size, choice, value, end;
    end = 0;
    //ask user to enter the length of the stack
    System.out.print("Please enter the length of the stack: ");
    size = key.nextInt();
    //declate and initialize the stack
    StackArray stack1 = new StackArray(size);
    //loop to continue operations
    while(end == 0)
    {
        //print out menu for commands
        System.out.println("t1) Push nt2) Pop nt3) Peek nt4) Size nt5) isEmpty nt6) End");
        System.out.print("Please choose an option: ");
        choice = key.nextInt();
        //switch the choice and execute commands
        switch (choice)
        {
            case 1: System.out.println("Please enter a value: ");
                    value = key.nextInt();
                    stack1.push(value);
                    stack1.toString();
                    break;
            case 2: stack1.pop();
                    stack1.toString();
                    break;
            case 3: stack1.peek();
                    stack1.toString();
                    break;
            case 4: System.out.println("Size: " + stack1.size());
                    stack1.toString();
                    break;
            case 5: if(!stack1.isEmpty())
                    {
                        System.out.println("Stack is empty.");
                    }
                    else
                        System.out.println("Stack is NOT empty.");
                    stack1.toString();
                    break;
            case 6: end = 1;
                    System.out.println("Goodbye!");
                    break;
        }
    }
}

}

代码有一些问题:

void peek() {
    //This was the change - you can peek as long as the stack
    // is NOT empty.
    if (!isEmpty())
        System.out.println(array[top]);
    else
        System.out.println("Stack is empty");
}

在这种状态下,您的堆栈是空的那一刻,您将出于一个例外。这很容易解决。另外,您的流行方法从根本上是错误的:

int pop()
{
    //declare local variable
    int temp;
    //if statement to check if stack is not empty
    if(!isEmpty())
    {
        temp = array[top]; //note the change here
        top--;
        length--;
    }
    else
        System.out.println("No more items in Stack.");
    return temp; //note the change here
}

另外 - 我建议tostring实现其原始目的 - 返回字符串表示,而不是打印出来。

public String toString()
{
    String returnStr = "Stack: [";
    for(int i = 0; i < length; i++)
    {
        returnStr+= array[i] + ", ";
    }
    returnStr += "]";
   return returnStr;
}

现在,只需编写System.out.println(array1);,而不是在主代码中编写array1.toString();。暗示ts肿。请注意,我更改了&lt; = in&lt;,现在您将在按下打印输出中的额外0!

另一个问题是您的主要案例语句:

 //The check here should be positive - say the stack is empty when it is!
 case 5: if(stack1.isEmpty()) {
             System.out.println("Stack is empty.");
         }
         else
             System.out.println("Stack is NOT empty.");

我认为这就是全部。

最新更新