Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

collections - Java Sorting object in ArrayList

Hi I have Card class... In another class I create an arrayList of Card objects. How would I go about sorting the arrayList based on the value of the card? The ace is the lowest card value and the king is the highest.

A,2,3,4,5,6,7,8,9,T,J,Q,K

public class Card {

        char rank, suit;

        public Card(char rank, char suit){
                this.rank = rank;
                this.suit = suit;
        }

        public void setCard(char rank, char suit){
                this.rank = rank;
                this.suit = suit;
        }

        public char getRank(){
                return rank;
        }

        public char getSuit(){
                return suit;
        }

        public void setRank(char rank){
                this.rank = rank;
        }

        public void setSuit(char suit){
                this.suit = suit;
        }


        public String toString(){
                String str = "";
                str += this.getRank();
                str += this.getSuit();
                return str;
        }

          public boolean equals(Object obj){
               Card card = (Card) obj;
               if(this.rank == card.getRank() && this.suit == card.getSuit()){
                   return true;
               }
               return false;
           }

    public boolean isValidCard(Card card){
        char s = card.getSuit();
        char r = card.getRank();
        if(s=='H' || s=='S' || s=='D' || s=='C'){
            if(r=='A' || r=='2' || r=='3' || r=='4' || r=='5' || r=='6' || r=='7' || 
                    r=='8' || r=='9' || r=='T' || r=='J' || r=='Q' || r=='K'){
                return true;
            }                   
        }
        return false;
     }

    public boolean allowedInHigherPiles(Card card, Game game, int pile){
        if(pile>=5 && game.getPile(pile).cards.size()==0){
                if(card.getRank()!='K')
                        return false;
        }
        return true;
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The code would be much cleaner if you use enum to represent rank and suite instead of char.

In fact, http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html has a Card sample illustrates use of Enum

The relevant code bit is copied below

public class Card implements Comparable, java.io.Serializable {
    public enum Rank { deuce, three, four, five, six, seven, eight, nine, ten,
                       jack, queen, king, ace }
    public enum Suit { clubs, diamonds, hearts, spades }

    private final Rank rank;
    private final Suit suit;

    private Card(Rank rank, Suit suit) {
        if (rank == null || suit == null)
            throw new NullPointerException(rank + ", " + suit);
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() { return rank; }
    public Suit suit() { return suit; }

    public String toString() { return rank + " of " + suit; }

    public int compareTo(Object o) {
        Card c = (Card)o;
        int rankCompare = rank.compareTo(c.rank);
        return rankCompare != 0 ? rankCompare : suit.compareTo(c.suit);
    }

    private static List<Card> sortedDeck = new ArrayList<Card>(52);
    static {
        for (Iterator<Rank> i = Rank.VALUES.iterator(); i.hasNext(); ) {
            Rank rank = i.next();
            for (Iterator<Suit> j = Suit.VALUES.iterator(); j.hasNext(); )
                sortedDeck.add(new Card(rank, j.next()));
        }
    }

    // Returns a shuffled deck
    public static List<Card> newDeck() {
        List<Card> result = new ArrayList<Card>(sortedDeck);
        Collections.shuffle(result);
        return result;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...