在测试类中编译此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
结尾为红色
我认为问题是我创建的默认构造函数,该构造函数几乎与其中pop,peek和push是Stack类的Stack类相同
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扩展了Stack实现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;
}
答案 0 :(得分:1)
您正在使用def make_greeting(name, greeting = "Hello"):
return (greeting + " " + name + "!")
# get name and greeting, send to make_greeting
print(make_greeting(get_name(), get_greeting()))
def get_name():
name_entry = input("enter a name: ")
return name_entry
def get_greeting():
greeting_entry = input("enter a greeting: ")
return greeting_entry
函数返回大小,但是大小始终被硬编码为getSize()
,这将导致52
循环尝试在没有可用空间时弹出更多内容
为什么您有一个实现for
的自定义类?它似乎并没有增加太多功能。
如果您想继续使用自定义类,则可以:
Stack<T>
为false时会不断弹出。isEmpty()
函数答案 1 :(得分:1)
您应该使用size()
类提供的Stack
函数,如果要强制执行最大限制,可以执行
public class DiscardPile<T> extends Stack<T> {
private static int MAX_SIZE = 52;
public T push(T mink) {
if (this.size() < this.MAX_SIZE) {
super.push(mink);
}
return mink;
}
}
答案 2 :(得分:1)
每个人都指出了尺寸问题。您可以遍历自定义Stack类。实现可迭代的界面
public class DiscardPile<T> extends Stack<T> implements Iterable<T> {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
// Implementation your iterator, methods like hasNext etc.
}
}
}
然后遍历堆栈
Iterator iterator = discardPile.iterator();
while (iterator.hasNext()) {
//your code
}