0

对于家庭作业,我得到了以下代码,其中方法为空白。我一直在研究它,但我仍然不明白 mutator setComparer 或 Comparator comparer 在这个程序中是如何工作的。我在网上研究了如何使用比较器,但这个想法仍然不清楚。谁能给我一些指导?

  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>();

你想要升序排序和降序排序。你所做的是你写到实现比较器的不同类:

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),在第二种情况下调用Collections.sort(your_collection,object of your_comparator)或 Arrays.sort(you_array,object of your collection )

使用 II'nd Version 总是更好,因为它没有为您的类的所有集合定义默认排序行为。

有关更多详细信息,请参阅Java 中的 Comparator 和 Comparable

于 2013-05-19T17:02:05.973 回答