Java Can';t在不破坏编译的情况下反转数组
目前,我正在尝试获取一个列表,以便在测试类中向后打印。我认为Collections.reverse(discardPile)可以工作,所以我使用了它,结果是混合的,至少可以这么说 这是到目前为止我的代码Java Can';t在不破坏编译的情况下反转数组,java,Java,目前,我正在尝试获取一个列表,以便在测试类中向后打印。我认为Collections.reverse(discardPile)可以工作,所以我使用了它,结果是混合的,至少可以这么说 这是到目前为止我的代码 DiscardPile<Card> discardPile = null; discardPile = new DiscardPile<Card>(52, 0); discardPile.push(new Car
DiscardPile<Card> discardPile = null;
discardPile = new DiscardPile<Card>(52, 0);
discardPile.push(new Card(8));
discardPile.push(new Card(32));
discardPile.push(new Card(48));
discardPile.push(new Card(2));
discardPile.push(new Card(17));
discardPile.push(new Card(20)); //removeTopCard should remove all that's above
discardPile.push(new Card(25));
discardPile.push(new Card(50));
discardPile.push(new Card(19));
discardPile.push(new Card(41)); //10 Cards that must be popped
//Collections.reverse(discardPile); //texted it out for the meantime
System.out.println(discardPile.toString());
Card[] cardArr = discardPile.removeTopCard(new Card(50));
for(Card co : cardArr) { //for loop to print the method removeTopCards
System.out.println(co);
}
}
然后丢弃。卸下OPCARD(新卡(50))进行打印
没有集合。反向(丢弃堆)
我的代码打印
[10 of Clubs
, 8 of Hearts
, J of Spades
, 4 of Clubs
, 6 of Diamonds
, 9 of Diamonds
, A of Diamonds
, K of Spades
, 8 of Diamonds
, 4 of Spades
]
4 of Spades
8 of Diamonds
K of Spades
public class Card {
private int rank;
private int suit;
public Card() {
rank = (int) (Math.random() * 13);
suit = (int) (Math.random() * 4);
}
public Card(int n) {
if (n >= 0 && n <= 51) {
rank = n % 13;
suit = n / 13;
}
}
public Card(int r, int s) {
if ((r >= 0 && r <= 12) && (s >= 0 && s <= 3)) {
rank = r;
suit = s;
}
}
public String toString() {
String c = getRankAsString() + " " + "of " + getSuitAsString() + "\n";
c = c.replace(",", "").replace("[", "").replace("]", "");
return c;
}
public void setRank(int r) {
if (r >= 0 && r <= 12)
rank = r;
}
public void setSuit(int s) {
if (s >= 0 && s <= 3)
suit = s;
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
public String getRankAsString() {
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; // 0-12
return ranks[rank];
}
public String getSuitAsString() {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; // 0-3
return suits[suit];
}
public boolean equals(Card otherCard) {
return ((rank == otherCard.rank) && (suit == otherCard.suit));
}
public int compareByRank(Card otherCard) {
return rank - otherCard.rank;
}
public int compareBySuit(Card otherCard) {
return suit - otherCard.suit;
}
}
它以正确的顺序打印所有括号内的数字,但下一系列字符串出现问题。它可以打印3张以上的卡片
这是我的卡片课
[10 of Clubs
, 8 of Hearts
, J of Spades
, 4 of Clubs
, 6 of Diamonds
, 9 of Diamonds
, A of Diamonds
, K of Spades
, 8 of Diamonds
, 4 of Spades
]
4 of Spades
8 of Diamonds
K of Spades
public class Card {
private int rank;
private int suit;
public Card() {
rank = (int) (Math.random() * 13);
suit = (int) (Math.random() * 4);
}
public Card(int n) {
if (n >= 0 && n <= 51) {
rank = n % 13;
suit = n / 13;
}
}
public Card(int r, int s) {
if ((r >= 0 && r <= 12) && (s >= 0 && s <= 3)) {
rank = r;
suit = s;
}
}
public String toString() {
String c = getRankAsString() + " " + "of " + getSuitAsString() + "\n";
c = c.replace(",", "").replace("[", "").replace("]", "");
return c;
}
public void setRank(int r) {
if (r >= 0 && r <= 12)
rank = r;
}
public void setSuit(int s) {
if (s >= 0 && s <= 3)
suit = s;
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
public String getRankAsString() {
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; // 0-12
return ranks[rank];
}
public String getSuitAsString() {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; // 0-3
return suits[suit];
}
public boolean equals(Card otherCard) {
return ((rank == otherCard.rank) && (suit == otherCard.suit));
}
public int compareByRank(Card otherCard) {
return rank - otherCard.rank;
}
public int compareBySuit(Card otherCard) {
return suit - otherCard.suit;
}
}
公共类卡{
私有整数秩;
私人诉讼;
公共卡(){
rank=(int)(Math.random()*13);
suit=(int)(Math.random()*4);
}
公共卡(int n){
如果(n>=0&&n=0&&r=0&&s=0&&r=0&&s据我所知,您希望使用堆栈数据结构
Stack stack = new Stack();
stack.push(discardPile.push(new Card(32))); .......
然后,您可以使用stack.peek()获得堆栈的顶部
和所有其他与堆栈相关的操作都可以高效地完成。如果您不了解堆栈数据结构,请按照本文档操作。请编辑您的问题,并添加DiscardPile和Card类。@Rcordoval嘿,我刚刚添加了您请求的类。stack
在java中已过时(class Stack extends Vector
)。应改用Deque
。
Stack stack = new Stack();
stack.push(discardPile.push(new Card(32))); .......