Java 如何将列表类型转换为双数组或整数数组?
所以我想整理一副牌。构造器DeckOfCards设置并洗牌一副牌。但这些卡存储在类型列表中,其中卡类型属于卡类。在调用DeckOfCards之后,我应该询问用户应该使用什么样的排序算法来对卡片组进行排序。例如,我刚刚在末尾加入了插入排序。但是insertionsort函数接受双数组。所以我需要将列表转换为双数组。我怎么做 代码:Java 如何将列表类型转换为双数组或整数数组?,java,arrays,list,Java,Arrays,List,所以我想整理一副牌。构造器DeckOfCards设置并洗牌一副牌。但这些卡存储在类型列表中,其中卡类型属于卡类。在调用DeckOfCards之后,我应该询问用户应该使用什么样的排序算法来对卡片组进行排序。例如,我刚刚在末尾加入了插入排序。但是insertionsort函数接受双数组。所以我需要将列表转换为双数组。我怎么做 代码: import java.util.List; 导入java.util.array; 导入java.util.Collections; 导入java.util.Scann
import java.util.List;
导入java.util.array;
导入java.util.Collections;
导入java.util.Scanner;
//类来表示一组卡中的一张卡
班级卡
{
公共静态枚举面{Ace,平分,三,四,五,六,
七,八,九,十,杰克,皇后,国王};
公共静态枚举套装{梅花、钻石、红心、黑桃};
私有最终面;//卡的面
私人最终诉讼;//信用卡诉讼
//双参数构造函数
公共卡(脸牌、西装牌)
{
face=cardFace;//初始化卡的面
suit=cardSuit;//初始化卡套
}//结束两个参数卡构造函数
//卡片的背面
公共脸getFace()
{
返回面;
}//结束方法getFace
//退卡
公诉状
{
反诉;
}//结束方法getSuit
//卡片的返回字符串表示法
公共字符串toString()
{
返回字符串。格式(“%s/s”,面,套装);
}//结束方法toString
}//末级卡
//信用证类别声明
公营甲板
{
私有列表;//声明将存储卡的列表
//设置一副牌并洗牌
公共甲板
{
卡片[]卡片组=新卡片[52];
int count=0;//卡数
//用卡片对象填充卡片组
对于(Card.Suit Suit:Card.Suit.values())
{
对于(Card.Face:Card.Face.values())
{
牌组[计数]=新卡(正面、套装);
计数++;
}//结束
}//结束
list=Arrays.asList(deck);//获取列表
集合。洗牌(列表);//洗牌牌组
}//结束DeckOfCards构造函数
//输出甲板
公共印刷卡(
{
//在两列中显示52张卡片
对于(int i=0;i=0&&list[k]>currentElement;k--){
列表[k+1]=列表[k];
}
//将当前元素插入列表[k+1]
列表[k+1]=currentElement;
}
}
}
如果您想要转换卡片
对象列表,即列表
,您可以使用列表
本身提供的方法轻松地将其更改为卡片
数组(即卡片[]
)
然而,你在问什么的时候有些困惑。您希望一个需要一个double
数组的方法突然神奇地知道如何处理Card
类型的对象。卡
怎么会突然表现得像一个双精度数字
您应该首先将方法insertionSort()
更改为先获取Card[]
数组,然后修改其内部操作,以便在Card
对象的属性上进行比较,而不是在double
原语上进行比较
另一方面,您可能有兴趣知道Java已经通过
数组
类提供了排序功能。你可以看看这个函数。但是,您仍然需要实现该接口,以便Java知道如何对数组进行排序,它不知道如何对卡
对象进行排序(按脸?按套装?按哪个顺序?Java不知道如何玩牌),因此,您需要使Card
对象实现compariable
,并使用一种方法帮助Java确定哪个Card
实例位于哪个实例之前。如果您仍然希望自己实现排序,并更改方法insertionSort()
以获取一个可比较的项数组,并在其中调用compareTo()
,以了解哪个项在什么之前。如果我理解正确,您希望使用不同的算法对卡进行排序。而您的算法实现只接受double
数组
你需要考虑两个问题:
- 卡片比较的规则,例如心牌>钻石牌
- 如果所有排序都是比较排序算法
如果第二个问题的答案是:是
我建议您停止考虑将卡阵列类型转换为双阵列。相反,让您的所有算法实现更通用,例如,接受Comparable[]arr
array,甚至是Card[]arr
。T
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;
}
}
}