0

Iterator クラスで何ができますか? 「DeckIterator は抽象的ではなく、java.util.Iterator の抽象メソッド remove() をオーバーライドしません」と表示されます

import java.util.*;
import java.io.Serializable;

public class Deck implements Iterable<Card>, Serializable {

// The field for a List
private List<Card> deck;

/**
 * Constructor creates the list, initializes all the cards in the deck and
 * shuffles them
 */
public Deck() {

    deck = new ArrayList<Card>();

    for (Card.Rank r : Card.Rank.values()) {
        for (Card.Suit s : Card.Suit.values()) {
            deck.add(new Card(r, s));
        }
    }

    shuffle();

    // Converting to a LinkedList
    List<Card> list = new LinkedList<Card>();
    for (int i = 0; i < deck.size(); i++) {
        list.add(deck.get(i));
    }
    deck = list;
}

/**
 * Method to randomize the cards
 */
public void shuffle() {

    Collections.shuffle(deck);
}

/**
 * Method that removes the top card from the deck
 *
 * @return the top card
 */
public Card deal() {

    return deck.remove(0);
}

/**
 * Number of cards remaining in the deck
 *
 * @return number of cards in the deck
 */
public int size() {

    return deck.size();
}

/**
 * Reinitializes the deck.
 */
public void newDeck() {

    List<Card> newList = new LinkedList<Card>();
    for (int i = 0; i < deck.size(); i++) {
        newList.add(deck.get(i));
    }
    deck = newList;

}

      public Iterator<Card> iterator(){

           Iterator<Card> aDeck = deck.iterator();
            return aDeck;
        }

「DeckIterator は抽象的ではなく、java.util.Iterator の抽象メソッド remove() をオーバーライドしません」と表示されます

public class DeckIterator implements Iterator<Card> {

    public List<Card> reverseIterator(){


        List<Card> reverseList = new LinkedList<Card>();
        for(int i = deck.size()-1; i >= 0; i--){
            reverseList.add(deck.get(i));
        }

        return reverseList;
    }

}


public static void main(String[] args){

    Deck myDeck = new Deck();
    System.out.println("The card " + myDeck.deal() + " is not in the deck now");

    }
}
4

1 に答える 1

1

メソッドnewDeck()は、新しいエントリを追加する前にリストをクリア/空にする必要があります。または、単にdeck新しい に再初期化しListます。実際、コンストラクター内のコードのほとんど (すべて?) はnewDeck()メソッドに移動する必要があり、コンストラクターはそれを簡単に呼び出すことができます。

于 2012-11-11T19:40:11.003 に答える