首页 > 解决方案 > StackEmptyException 异常问题

问题描述

在我的测试类中编译这个 for 循环时遇到问题。

在测试类 LabApp 中,我目前有

import generics.StackFullException;
import generics.StackEmptyException;


public class Lab4App {
       public static void main(String[] args)throws StackFullException, StackEmptyException {
        try {
        DiscardPile<Card> discardPile = null; 
        discardPile = new DiscardPile<Card>();
        discardPile.push(new Card(8));
        discardPile.push(new Card(32));
        discardPile.push(new Card(48));
        discardPile.push(new Card(2));
        discardPile.push(new Card(17));
        discardPile.push(new Card(20)); //removeTopCard should remove all that's above
        discardPile.push(new Card(25));
        discardPile.push(new Card(50));
        discardPile.push(new Card(19));
        discardPile.push(new Card(41)); //10 Cards that must be popped

        for(int i = 0; i < discardPile.getSize(); i++) {
            Card var = discardPile.pop(); //pops the cards that are above
                System.out.println(var.getRankAsString() + " of " + var.getSuitAsString());
        }

        }
        catch (StackEmptyException SEE) {
            System.out.println("StackEmptyException: " + SEE.getMessage());
        }
        catch (StackFullException SFE) {
            System.out.println("StackFullException: " + SFE.getMessage());
       }
}
}

它正在打印所需的输出

4 of Spades
8 of Diamonds
K of Spades
A of Diamonds
9 of Diamonds
6 of Diamonds
4 of Clubs
J of Spades
8 of Hearts
10 of Clubs

但它返回

Exception in thread "main" java.util.EmptyStackException

最后是红色的

我认为问题是我创建的默认构造函数几乎与 Stack 类的构造函数相同,其中 pop、peek 和 push 是

public class DiscardPile<T> extends Stack<T>  { //subclass of its parent Stack
    private T[] data;
    private int size;


    //private static final int maxSize = 52;

    public DiscardPile() throws StackFullException, StackEmptyException {
        //this.data = (T[]) new Object[maxSize];
        this.size = 52; //52 is supposed to be the max value
    }
    /**
     * Constructs a new Stack with capacity specified by user
     * @param size the size of the Stack
     */
    public DiscardPile(int size){
        //this.data = (T[]) new Object[size];
        this.size = 0;
    }

    public int getSize(){ // getter
        return this.size;
    }

如果我输入 10 而不是 52,它工作得很好,最后没有错误(因为有 10 张卡),如果我输入 0,它就不会打印。零应该是初始值。

任何帮助将不胜感激!

编辑:公共类 DiscardPile 扩展堆栈实现 Iterable {

//subclass of its parent Stack

    //private T[] data;
    private int size;


    private static final int MAX_SIZE = 52;

    public T push(T mink) {
        if (this.size() < this.MAX_SIZE) {
            super.push(mink);
        }
        return mink;
    }

    public DiscardPile() throws StackFullException, StackEmptyException {
        //this.data = (T[]) new Object[MAX_SIZE];
        this.size = 10; 
    }

    public DiscardPile(int size){
        //this.data = (T[]) new Object[size];
        this.size = 0;
    }

    public int getSize(){ // getter
        return this.size;
    }

标签: javaexceptionconstructor

解决方案


您正在使用该getSize()函数来返回大小,但是大小始终是硬编码的52,这会导致您的for循环在没有可用的情况下尝试并弹出更多。

为什么你有一个实现的自定义类Stack<T>?它似乎没有增加太多功能。

如果您想继续使用您的自定义类,您可以:

  • isEmpty()使用一个 for 循环,它在为 false时不断弹出。
  • 使用size()默认位于 Stack 类中的函数
  • 在您的自定义类中编写一些内容,以便在您添加新项目时跟踪大小。

推荐阅读