[英]How to convert a list type to double array or int array?
So I want to sort a deck of cards. 所以我想整理一副纸牌。 The constructor DeckOfCards sets up and shuffles a deck of cards.
构造函数DeckOfCards设置并随机播放一副纸牌。 But the cards are stored in a list of type List where Card type belongs to Card class.
但是卡存储在列表类型列表中,其中卡类型属于卡类。 After calling DeckOfCards I should ask the user what kind of sorting algorithm should be used to sort the deck of cards.
调用DeckOfCards之后,我应该问用户应该使用哪种排序算法对卡片组进行排序。 I have just included Insertion sort at the end for example.
例如,我刚刚在最后添加了插入排序。 But the insertionsort function takes in a double array.
但是insertssort函数采用双精度数组。 So I need to convert List list to a double array.
所以我需要将列表列表转换为双精度数组。 How can I do it?
我该怎么做?
Code: 码:
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;
}
}
}
If you wanted to convert a list of Card
objects, ie List<Card> list
you could easily change it to a Card
array (ie Card[]
) using a method provided by List
itself called toArray
如果要转换
Card
对象的列表,即List<Card> list
,则可以使用List
本身提供的方法toArray
轻松将其更改为Card
数组(即Card[]
)。
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
. 您期望的是一个突然出现需要
double
数组的方法,该方法神奇地知道如何处理Card
类型的对象。 How can a Card
suddenly behave like a double precision number? Card
如何突然表现为双精度数字?
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. 您应该先更改
insertionSort()
方法insertionSort()
可能是从某个地方复制而未尝试了解其作用),以首先获取Card[]
数组,然后在内部修改其作用,以便比较对象的属性。 Card
对象而不是double
原语。
On the other hand, you might be interested to know that Java already provides sorting functions through the Arrays
class. 另一方面,您可能想知道Java已经通过
Arrays
类提供了排序功能。 You can have a look at the function Arrays.sort()
. 您可以看一下
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. 但是,您仍然需要实现
Comparable
接口,以便Java知道如何对数组进行排序,不知道应该如何对Card
对象进行排序(按面?按西装?按什么顺序排序?),Java不知道如何玩卡),因此您需要使您的Card
对象实现Comparable
并具有compareTo()
方法,该方法可以帮助Java确定哪个Card
实例位于哪个Card
实例之前。 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. 如果您仍然想自己实现排序并更改方法
insertionSort()
以获取可Comparable
项的数组,则可以使用相同的方法,并在其中调用compareTo()
以知道哪个项在什么之前。
If I understood right, you want to sort your Cards with different algorithms. 如果我理解正确,您想使用不同的算法对卡进行排序。 And your implementation of algorithms accept only
double
array. 而且算法的实现仅接受
double
数组。
Two question you need to think about: 您需要考虑的两个问题:
The rule of Card comparison, eg Heart Ace > Diamonds Ace? 卡片比较的规则,例如,“ Heart Ace”>“ Diamonds Ace”?
if all sortings are comparison sort algorithm? 如果所有排序都是比较排序算法?
If the answer of the 2nd question is: YES 如果第二个问题的答案是: 是
I suggest you stopping thinking about converting Card Array type to double array. 我建议您不要再考虑将Card Array类型转换为double array。 Instead, making your all algorithm implementation more generic, for example, accept
Comparable[] arr
array, or even Card[] arr
. 相反,使所有算法的实现更通用,例如,接受
Comparable[] arr
数组,甚至接受Card[] arr
。 Then make your Card
class implement Comparable interface. 然后使您的
Card
类实现Comparable接口。 Implement your card comparison rule in compareTo(Card c)
method. 在
compareTo(Card c)
方法中实现卡比较规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.