堆栈数据结构中的Bounds异常索引

时间:2013-10-13 20:20:33

标签: java arrays data-structures stack computer-science

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

我不断获得索引超出范围的异常;调试让我怀疑它与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;
}

}

非常感谢!

JLL

3 个答案:

答案 0 :(得分:3)

Stack(int)构造函数

maxSize = stackMaxSize;

应该是

stackMaxSize = maxSize;

答案 1 :(得分:2)

修正后的工作代码如下:(注意,主要功能是用Stack类编写的,为简单起见,修正了代码行的注释)

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

答案 2 :(得分:0)

原始程序的问题是: 1)For循环应该是:

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

而不是&lt; =(感谢ovunccetin)

2)在Stack类的构造函数中

maxSize = stackMaxSize;

应该是:

stackMaxSize = maxSize;

感谢Ravi Thapliyal