2013-05-21 2 views
-5

나는 카드의 arraylist을 가지고 있습니다 ArrayList hand; 손 = {2S, 3H, 2D, 6H, 4C}이라고 가정합니다. 나는 arrabylist 손을 정렬하여 hand = {4C,2D,3H,6H,2S}이되는 방법이 필요합니다. enum에 메서드 compareTo()이 있다는 것을 알고 있지만 사용 방법을 잘 모르겠습니다. 아무도 나에게 약간의 지침을 줄 수 있습니까?열거 형을 사용하여 arraylist를 정렬하는 방법

public class PlayingCard implements Comparable 
{ 
//Instance Variables 
private Suit suit; 
private Rank rank; 

//Constructor 
public PlayingCard(Suit suit, Rank rank) 
{ 
    this.suit = suit; 
    this.rank = rank; 
} 
.... 
public int compareTo(Object other) 
{ 
    PlayingCard that = (PlayingCard)other; 

    if (this.suit == that.suit) 
    return -this.rank.compareTo(((PlayingCard)other).rank); 

    return -this.suit.compareTo(((PlayingCard)other).suit);   
} 

//============================================================================ 
//Representation of the Suit of a Playing-Card 
public enum Suit 
{ 
    CLUBS('C'), DIAMONDS('D'), HEARTS('H'), SPADES('S'); 

    private char symbol; 

    private Suit(char symbol) 
    { 
     this.symbol = symbol; 
    } 

    public char getSymbol() 
    { 
     return this.symbol; 
    } 
} 

//============================================================================ 
//Representation of the Rank os a Playing-Card 
public enum Rank 
{ 
    DEUCE('2'), TREY('3'), FOUR('4'), FIVE('5'), SIX('6'), SEVEN('7'), 
    EIGHT('8'), NINE('9'), TEN('T'), JACK('J'), QUEEN('Q'), KING('K'), ACE('A'); 

    private char symbol; 

    private Rank(char symbol) 
    { 
     this.symbol = symbol; 
    } 

    public char getSymbol() 
    { 
     return this.symbol; 
    } 
} 
} 

-------------------------------------------------------------------------------------- 
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) { 

    if (handSize < 1) { 
     throw new RuntimeException("Hand size cannot be less than 1"); 
    } 
    cardsInCompleteHand = handSize; 
    hand = new ArrayList<PlayingCard>(); 

} 
... 
public void sortCards() 
{ 
} 
+1

카드를 나타내는 개체가 있습니까? 카드의 구성 요소를 나타내는 두 개의 열거 형을 보여 주셨습니다. –

+0

예, 카드를 나타내는 객체가 – jorgeAChacon

+0

@ user1166061입니다. 코드를 표시 할 수 있습니까? – mre

답변

3

당신은 Rank에 의해 처음으로 정렬합니다 CardcompareTo() 방법을 추가하고, Rank 경우 Suit에 의해 다음, 동일합니다. 우리가 Guava를 사용하는 경우, 그것은 매우 간단합니다

public class Card implements Comparable<Card> 
{ 
    private Rank rank; 
    private Suit suit; 

    ... 

    public int compareTo(Card that) 
    { 
    return ComparisonChain.start() 
     .compare(this.rank, that.rank) 
     .compare(this.suit, that.suit) 
     .result(); 
    } 
} 

여기 ComparisonChain를위한 JavaDoc입니다. 우리가 List<Card>이라고 가정하면

는, 당신은 Collections.sort(hand)를 사용하여 목록을 정렬 할 수 있습니다.

+0

이전에 전체 코드를 게시하지 않아서 죄송합니다. 예 손은 Arraylist 이며 Collections.sort (손) – jorgeAChacon

+0

@ user1166061을 사용하여 손을 정렬해야합니다.이 질문에 대한 답변이 있습니까? 그렇다면 답을 표시해야합니다. –

+0

아니요, 그렇지 않았습니다. 나는 나머지 코드를 게시하여 누군가 내가 sort 메소드를 작성하는 방법을 찾도록 도와 줄 수 있는지 알아 보았다. – jorgeAChacon

관련 문제