0

我正在编写一个程序来使用堆栈数据结构反转单词。它应该工作的方式是我输入一个字符串,将字符串的每个字符插入一个堆栈对象中,然后我会将每个对象从堆栈中弹出并打印它们。该单词将与原始输入的顺序相反,因为这正是堆栈的工作方式。

我不断得到索引越界异常;调试让我怀疑它与 Stack 类中的初始数组初始化有关,但它也可能与 push() 函数有关。

这是整个代码:

public class Stack      // object to emulate stack data structure
{
private int stackMaxSize;
private char stackArray[];
private int currentSize;

public Stack()    // if initialized without any parameters
{
    this(100);
}

public Stack(int maxSize)      // if initialized with parameter
{
    maxSize = stackMaxSize;
    stackArray = new char[stackMaxSize];
    currentSize = -1;
}

public void push(char c)   //pushes new character into stack
{
    stackArray[++currentSize] = c;
}

public char pop()     //pops character out of stack
{
    return stackArray[currentSize--];
}

public char peek()      // returns character on top of stack
{
    return stackArray[currentSize];
}

public boolean isEmpty()      // returns whether stack is empty or not
{
    return (currentSize < 0);
}
}

这是主要的:

import java.util.Scanner;
public class ReverseWord
{
public static void main(String[] args)
{
   Stack wordStack = new Stack(100); // default size is 100

   System.out.print("Enter the word to be reversed: ");
   String word = getString();

    for (byte i = 0; i <= word.length(); i++)    // inserts word into stack char by char
    {
        wordStack.push(word.charAt(i));
    }

    System.out.print(wordStack.pop());


}

static String getString()
{
     Scanner input = new Scanner(System.in);
     String s = input.nextLine();
     return s;
}

}

非常感谢!

仲量联行

4

3 回答 3

3

在你的Stack(int)构造函数中

maxSize = stackMaxSize;

应该

stackMaxSize = maxSize;
于 2013-10-13T20:26:01.963 回答
2

The corrected and working code as follows: (Note that, the main function is written in Stack class for simplicity and corrected code lines are commented)

public class Stack // object to emulate stack data structure
{
    private final int  stackMaxSize;
    private final char stackArray[];
    private int        currentSize;

    public Stack() // if initialized without any parameters
    {
        this(100);
    }

    public Stack(final int maxSize) // if initialized with parameter
    {
        this.stackMaxSize = maxSize; /* corrected: assignment reversed */
        this.stackArray = new char[this.stackMaxSize];
        this.currentSize = -1;
    }

    public void push(final char c) // pushes new character into stack
    {
        this.stackArray[++this.currentSize] = c;
    }

    public char pop() // pops character out of stack
    {
        return this.stackArray[this.currentSize--];
    }

    public char peek() // returns character on top of stack
    {
        return this.stackArray[this.currentSize];
    }

    public boolean isEmpty() // returns whether stack is empty or not
    {
        return this.currentSize < 0;
    }

    public static void main(final String[] args) {
        Stack wordStack = new Stack(100); // default size is 100

        System.out.print("Enter the word to be reversed: ");
        String word = getString();

        /* corrected: i <= word.length() >> i < word.length() */
        for (byte i = 0; i < word.length(); i++) // inserts word into stack char by char
        {
            wordStack.push(word.charAt(i));
        }

        /* corrected: while loop added to consume the entire stack */
        while (!wordStack.isEmpty()) {
            System.out.print(wordStack.pop());
        }
    }

    static String getString() {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        return s;
    }
}
于 2013-10-13T20:33:26.307 回答
0

原始程序的问题在于:1)for循环应该是:

  for (byte i = 0; i < word.length(); i++)

而不是 <= (感谢 ovunccetin)

2)在Stack类的构造函数中

maxSize = stackMaxSize;

应该:

stackMaxSize = maxSize;

感谢 Ravi Thapliyal

于 2013-10-13T21:17:47.077 回答