0

所以我想整理一副牌。构造函数 DeckOfCards 设置并洗牌一副牌。但是卡片存储在 List 类型的列表中,其中 Card 类型属于 Card 类。在调用 DeckOfCards 之后,我应该询问用户应该使用哪种排序算法来对卡片组进行排序。例如,我刚刚在末尾包含了插入排序。但是插入排序函数接受一个双精度数组。所以我需要将 List 列表转换为双数组。我该怎么做?

代码:

import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

// class to represent a Card in a deck of cards
class Card 
{    
public static enum Face { Ace, Deuce, Three, Four, Five, Six,
  Seven, Eight, Nine, Ten, Jack, Queen, King  };
public static enum Suit { Clubs, Diamonds, Hearts, Spades };

private final Face face; // face of card
private final Suit suit; // suit of card

// two-argument constructor
public Card( Face cardFace, Suit cardSuit ) 
{   
   face = cardFace; // initialize face of card
   suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor

// return face of the card
public Face getFace() 
{ 
  return face; 
} // end method getFace

// return suit of Card
public Suit getSuit() 
{ 
  return suit; 
} // end method getSuit

// return String representation of Card
public String toString()
{
   return String.format( "%s of %s", face, suit );
} // end method toString
} // end class Card

// class DeckOfCards declaration
public class DeckOfCards 
{
private List<Card> list; // declare List that will store Cards

// set up deck of Cards and shuffle
public DeckOfCards()
{
  Card[] deck = new Card[ 52 ];
  int count = 0; // number of cards

  // populate deck with Card objects
  for ( Card.Suit suit : Card.Suit.values() )  
  {
     for ( Card.Face face : Card.Face.values() )   
     {
        deck[ count ] = new Card( face, suit );
        count++;
     } // end for
  } // end for

  list = Arrays.asList( deck ); // get List
  Collections.shuffle( list );  // shuffle deck
} // end DeckOfCards constructor

// output deck
public void printCards()
{
  // display 52 cards in two columns
  for ( int i = 0; i < list.size(); i++ )
     System.out.printf( "%-20s%s", list.get( i ),
        ( ( i + 1 ) % 2 == 0 ) ? "\n" : "" );
} // end method printCards

public static void main( String args[] )
{
  DeckOfCards cards = new DeckOfCards();
  cards.printCards();
  //add code here to take input from user and sort
  int a;
  System.out.println("\nSort the deck of cards");
  System.out.println("\nEnter your choice: \n1.Selection Sort \n2.Insertion Sort 
\n3.Merge       Sort\n");
  //take input
  Scanner reader = new Scanner(System.in);
  a=reader.nextInt();
  switch(a)
  {
    case 1:
        //call Selection sort
        break;
    case 2:
        //call Insertion sort
        break;
    case 3:
        //call Merge sort
        break;
  }
 } // end main  

//Insertion sort

} // end class DeckOfCards
public class InsertionSort {
/** The method for sorting the numbers */
public static void insertionSort(double[] list) {
for (int i = 1; i < list.length; i++) {
  /** insert list[i] into a sorted sublist list[0..i-1] so that
       list[0..i] is sorted. */
  double currentElement = list[i];
  int k;
  for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
    list[k + 1] = list[k];
  }

  // Insert the current element into list[k+1]
  list[k + 1] = currentElement;
}
}
}
4

2 回答 2

0

If you wanted to convert a list of Card objects, i.e. List<Card> list you could easily change it to a Card array (i.e. Card[]) using a method provided by List itself called toArray

However, you have some confusion in what you're asking. You are expecting that a method that expects an array of double suddenly magically knows how to treat objects of type Card. How can a Card suddenly behave like a double precision number?

You should change the method insertionSort() first (which you probably copied from somewhere without trying to understand what its doing) to take a Card[] array first, and then modify what its doing internally so that the comparison happens on the properties of the Card object rather than the double primitive.

On the other hand, you might be interested to know that Java already provides sorting functions through the Arrays class. You can have a look at the function Arrays.sort(). However you still need to implement the Comparable interface so that Java knows how to sort the array, it doesn't know how your Card objects should be sorted (by face? by suit? in which order? Java doesn't know how to play cards), so you need to make your Card objects implement Comparable and have a compareTo() method which helps Java decide which Card instance comes before which. You can use the same approach if you still want to implement the sorting yourself and change the method insertionSort() to take an array of Comparable items and inside it you call compareTo() to know which item comes before what.

于 2013-02-11T00:01:54.337 回答
0

如果我理解正确,您想使用不同的算法对卡片进行排序。并且您的算法实现只接受double数组。

你需要思考的两个问题:

  • 纸牌比较规则,例如 Heart Ace > Diamonds Ace?

  • 如果所有排序都是比较排序算法?

如果第二个问题的答案是: YES

我建议您停止考虑将 Card Array 类型转换为 double 数组。相反,让你的所有算法实现更通用,例如,接受Comparable[] arr数组,甚至Card[] arr. 然后让你的Card类实现 Comparable 接口。在方法中实现您的卡片比较规则compareTo(Card c)

于 2013-02-11T00:05:26.277 回答