执行我的程序时,它在第 13 行一直显示“null”我想知道我的算法出了什么问题,因为它一直打印 null。
private class SpadeIterator implements Iterator<Card>{
private int nextCardSpade;
private List<Card> cards;
private int count=0;
private SpadeIterator(List cards) {
this.cards=cards;
this.nextCardSpade = cards.size()-1;
}
@Override
public boolean hasNext() {
count++;
if(nextCardSpade<0)
return false;
//nextCardSpade--;
return true;
}
@Override
public Card next() {
int i=0;
this.count=i;
Card temp = cards.get(nextCardSpade);
while(hasNext()){ //find SPADES
temp=cards.get(nextCardSpade--);
i++;
if(temp.suit.value == Suit.SPADES.value)
return temp;
}
//DONT MOVE
return null;
//nextCardSpade--; //DONT DELETE
}
}
结果旨在显示 13 个黑桃,最后不返回 null。