我想对我的 Deque<Card>
的内容进行排序使用Card
收集类'getRealValue()
方法。
public class Card implements Comparable<Card> {
private final int value;
private final CardType cardType;
public Card(int value, CardType cardType) {
this.value = value;
this.cardType = cardType;
}
public int getRealValue() {
int realValue = this.value == 1 ? 52 : 0;
return realValue + this.value * 4 + this.cardType.ordinal();
}
public int compareTo(Card o) {
return this.getRealValue() - o.getRealValue();
}
}
这是我的 CardType 枚举
public enum CardType {
CLUB("♣"),
SPADE("♠"),
HEART("♥"),
DIAMOND("♦");
public final String icon;
private CardType(String icon) {
this.icon = icon;
}
}
我想根据realValue()
对我的双端队列进行排序
最佳答案
您始终可以清除它并按正确的顺序重新插入元素:
Card[] a = deque.toArray(new Card[0]);
Arrays.sort(a);
deque.clear();
for (Card card : a) {
deque.add(card);
}
记住这一点的表现。如果您的结构需要排序,请考虑使用 PriorityQueue
。
关于java - 排序 java.util.Deque,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395129/