【发布时间】:2014-08-26 14:39:23
【问题描述】:
我正在尝试解决一个为我提供以下课程的问题。如何创建自定义迭代器类 DeckIterator 来帮助我迭代 Deck 类中的 Card[] deck 数组?
我有一个如下所示的 Card 类:
public class Card {
private char suit;
private int value;
public Card (char s, int v)
{
this.suit = s;
this.value = v;
}
}
我有一个像这样的 Deck 类:
public class Deck {
private Card[] deck;
private int idx;
public Deck (int size)
{
this.deck = new Card[size];
this.idx = 0;
}
}
非常感谢
【问题讨论】:
-
你有什么尝试吗?
-
你应该正确实现Iterable接口。 tutorials.jenkov.com/java-generics/implementing-iterable.html
-
@OliCharlesworth 我做到了,我知道我应该在 DeckIterator 类中实现可迭代和在 DeckIterator 类中的迭代器,但我不知道如何使用它在作为数组的卡片组中进行迭代。 ..:/请帮帮我
-
为什么不使用 List
而不是数组?迭代器随列表免费提供 -
@wastl 我知道,但我正在尝试解决一个为我提供这些课程的问题,我需要使用我所拥有的。