0

宿題として、メソッドが空白の次のコードが与えられました。私はそれに取り組んできましたが、ミューテーター setComparer または Comparator 比較器がこのプログラムでどのように機能するかまだわかりません。コンパレータの使用方法をオンラインで調査しましたが、アイデアはまだ不明です。誰かが私に何らかのガイダンスを与えることができますか?.

  1. Comparator 比較子を初期化する必要がありますか? もしそうなら、どうすればいいですか?
  2. private int compare(PlayingCard one, PlayingCard two) の上のコメントは何を意味していますか?(this.comparer=null)

ありがとう

import java.util.*;
//Class to represent a "generic" hand of playing-cards
public class PlayingCardHand {
//Instance Variables

private int cardsInCompleteHand;     //Maximum # cards in this hand
private ArrayList<PlayingCard> hand; //A hand of Playing-Cards
private Comparator comparer;         //Client-provided comparison of PlayingCards

//Constructor
//Appropriate when PlayingCard compareTo() is to be used to compare PlayingCards
public PlayingCardHand(int handSize) {
    cardsInCompleteHand = handSize;
    hand = new ArrayList<PlayingCard>();

}

//Helper: Compare 2 PlayingCards
// if this.comparer is null, comparison uses compareTo()
//                           otherwise the Comparator is applied
private int compare(PlayingCard one, PlayingCard two) {

    return 0;
}

//Accessor: return # of cards currently in this hand
public int getNumberOfCards() {
    return cardsInCompleteHand;
}

public boolean isComplete() {
    if (hand.size() == cardsInCompleteHand) {
        return true;
    }
    return false;
}

//Accessor: return COPIES of the cards in this hand
public PlayingCard[] getCards() {
    PlayingCard[] temp = new PlayingCard[hand.size()];

    for (int i = 0; i < hand.size(); i++)//ch
    {

        temp[i] = hand.get(i);
    }
    return temp;
}

//Mutator: allows a client to provide a comparison method for PlayingCards
public void setComparer(Comparator comparer) {
}

//Mutator: Append a new card to this hand
public void appendCard(PlayingCard card) {
    int counter = 0;
    PlayingCard.Suit su = card.getSuit();
    PlayingCard.Rank ra = card.getRank();

    PlayingCard temp3 = new PlayingCard(su, ra);

    //10 20 goes here 30 40 if insert 25
    for (int i = 0; i < hand.size(); i++) {
        PlayingCard temp4 = hand.get(i);
        PlayingCard.Suit sui = temp4.getSuit();
        PlayingCard.Rank ran = temp4.getRank();
        if (su.ordinal() <= sui.ordinal() && ra.ordinal() <= ran.ordinal()) {
            hand.add(i, temp3);
            counter++;
        }

    }
    while (counter == 0) {
        hand.add(temp3);
    }
}
4

2 に答える 2

1

基本的に、同じタイプのオブジェクト間で異なるタイプの比較を行いたい場合は、 Comparator を使用します。たとえば、あなたが持っている

List<Integer> list = new ArrayList<Integer>();

昇順ソートと降順ソートの両方が必要です。あなたがすることは、Comparator を実装するさまざまなクラスに書き込むことです。

public class Ascending implements Comparator<Integer>{

   @Override
   public int compare(Integer o1, Integer o2) {
       return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
   }
}
public class Descending implements Comparator<Integer>{

   @Override
   public int compare(Integer o1, Integer o2) {
       return (o1<o2 ? -1 : (o1==o2 ? 0 : 1));
   }
}

次に、配列をソートできます。

Collections.sort(list, new Descending());

あなたの質問への答え:

1- クラス PlayingCardHand の使用方法によって異なります。私が見るところ、初期化する必要があります。

2- コメントは、PlayingCardHand を使用しているコードが、使用する並べ替え方法を決定することを意味します。

于 2013-05-19T16:44:40.693 に答える
1

常に2つのオプションが利用可能です

1.あなたのクラスは同等のインターフェースを実装しているため、compareTo()メソッドの実装を提供します。

2. コンパレータ インターフェイスを実装する独自のコンパレータ クラスを作成し、compare()メソッドの実装を提供して、独自のコンパレータを作成します。

最初のケースでは単にCollections.sort(your_collection)または Arrays.sort(your_array) を呼び出す必要があり、2 番目のケースではCollections.sort(your_collection,object of your_comparator)または Arrays.sort(you_array,object of your collection ) として呼び出します)

クラスのすべてのコレクションのデフォルトの並べ替え動作を定義しないため、常に II'nd バージョンを使用することをお勧めします。

詳細については、Java の Comparator および Comparable を参照してください。

于 2013-05-19T17:02:05.973 に答える