有没有办法按特定的非字母顺序对字符串数组进行排序?

时间:2014-05-07 21:20:40

标签: java arrays sorting

所以我正在制作一个扑克计划,然后通过Ace对卡片进行分类。我目前正在将面部卡转换为数值并使用Arrays.sort(),但它在代码后面给了我一些小问题。有没有办法构建一个键来对阵数组?谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

卡片有等级和套装。 Java枚举可以被赋予超出其序数值的值。也许你可以利用它?当你的抽象只影响一个游戏时,这并不是那么困难,但是尝试制作一个真正抽象的Card and Deck,特别是关于排名,会变得有点棘手。考虑一下黑杰克;这个Ace值10或1吗?这取决于。无论如何,请注意下面主要内容Collections.sort的内容。想想看,卡片和纸牌游戏在对象设计中做了很好的练习。对此进行建模的方法不止一种。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Card {
    public static enum Rank {
        One( 1 ), Two( 2 ), Three( 3 ), Four( 4 ),
        Five( 5 ), Six( 6 ), Seven( 7 ), Eight( 8 ),
        Nine( 9 ), Ten( 10 ), Jack( 11 ), Queen( 12 ),
        King( 13 ), Ace( 14 );

        public final int value;

        private Rank( int value ) {
            this.value = value;
        }
    }

    public static enum Suit {
        Spade, Heart, Diamond, Club
    }

    public final Rank rank;
    public final Suit suit;

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

    public String toString() {
        StringBuilder sb = new StringBuilder( this.getClass().getSimpleName() );
        sb.append( "( rank: " + rank );
        sb.append( ", suit: " + suit );
        sb.append( ")" );
        return sb.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ( ( rank == null ) ? 0 : rank.hashCode() );
        result = prime * result + ( ( suit == null ) ? 0 : suit.hashCode() );
        return result;
    }

    @Override
    public boolean equals( Object obj ) {
        if ( this == obj ) {
            return true;
        }
        if ( obj == null ) {
            return false;
        }
        if ( getClass() != obj.getClass() ) {
            return false;
        }
        Card other = (Card) obj;
        if ( rank != other.rank ) {
            return false;
        }
        if ( suit != other.suit ) {
            return false;
        }
        return true;
    }

    public static void main( String[] args ) {
        Card aceOfSpades = new Card( Rank.Ace, Suit.Spade );
        System.out.println( "aceOfSpades: " + aceOfSpades );
        System.out.println( "aceOfSpades.rank.ordinal: " + aceOfSpades.rank.ordinal() );
        System.out.println( "aceOfSpades.rank.value: " + aceOfSpades.rank.value );

        List<Card> deck = new ArrayList<Card>();
        for ( Suit suit : Suit.values() ) {
            for ( Rank rank : Rank.values() ) {
                deck.add( new Card( rank, suit ) );
            }
        }

        System.out.println( "deck: " + deck );

        Collections.sort( deck, new Comparator<Card>() {
            public int compare( Card c1, Card c2 ) {
                int result = c1.rank.value - c2.rank.value;
                return result;
            }
        } );

        System.out.println( "deck: " + deck );
    }

}

答案 1 :(得分:0)

您可以使用Map<String, String>吗?