Java对ArrayList中的对象进行排序 [英] Java Sorting object in ArrayList

查看:154
本文介绍了Java对ArrayList中的对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi我有Card类...在另一个类中,我创建一个array对象的arrayList。我将如何根据卡的值排序arrayList?王牌是最低的牌价,国王是最高的。



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

  public class Card {

char rank,suit;

公共卡(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;
}

}


解决方案

一个选项是实现Comparable接口,然后覆盖compareTo
一旦你这样做,排序列表很容易与Collections.sort(myCollection);



您可能会更好地避免实施Comparable并创建比较器对象,并且有一个版本的Collections.sort接受比较器。



您的比较功能可以简单地检查卡的等级,并在忽略西装时返回结果。



您可能想阅读这一切的Java教程订阅业务



更新:Bjorn正确地指出,当类具有自然排序顺序时应使用Comparable。我个人的看法是,卡不是真正的自然秩序,因为不同的游戏在Ace的解释不同,所以最好避免通过提供Comparable作为类的一部分分配语义。


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;
    }

}

解决方案

One option is to implement the Comparable interface and then override compareTo Once you've done that, sorting the list is easy with Collections.sort(myCollection);

You may be better of avoiding implementing Comparable and create a Comparator object, and there's a version of Collections.sort that takes the comparator.

Your comparison function can can then simply check the rank of the cards, and return the result while ignoring the suit.

You may want to read the Java tutorial on all this ordering business.

Update: Bjorn points out correctly that Comparable should be used when the class has a natural sorting order. My personal view is that for cards there isn't really a "natural order" since different games differ in their interpretation of the Ace, so it might be better to avoid assigning "semantics" by offering Comparable as part of the class.

这篇关于Java对ArrayList中的对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆