排序java.util.Deque

时间:2019-05-31 12:26:06

标签: java collections arraydeque

我想使用 body { background-color: lightpink; } h1 { color: black; text-shadow: 3px 2px grey; font-size: 60px; text-align: center; } h2 { color: black; margin-left: 20px; text-decoration: underline; text-transform: uppercase; text-align: center; font-size: 35px; text-shadow: 2px 1px grey; } h3 { color: black; font-size: 25px } h4 { color: black; margin-left: 20px; margin-right: 20px; font-family: "Times New Roman", Times, serif; text-align: center; } body { background-image: url("Graphics/background2.jpg"); } #para1 { text-align: center; color: red; } .lightgrey { background-color: lightgrey; } .padding { border: 3px solid black; padding: 1px 125px 1px 125px; background-color: grey; } .footer { border: 3px solid black; padding: 1px 125px 1px 125px; background-color: grey; } div.picture { border: 2px solid #000000; width: 600px; height: 4oopx } div.picture:hover { border: 2px solid #000000; } div.picture img { width: 100%; height: auto; } div.imagedescription { padding: 2px; text-align: centre; background-color: lightgrey; } .site-info::after { content: "Copyright Hisham Echafaki 2017 - All Rights Reserved "; } .parastyle1 { font-family: Arial, Helvetica, sans-serif; color: black; font-size: 28px; font-weight: bold; } .parastyle2 { font-family: Arial, Helvetica, sans-serif; color: black; font-size: 20px; font-weight: bold; } .box { position: relative; } .yeet_time { position: absolute; bottom: 0; right: 0; } p { margin-left: 150px; margin-right: 150px; font-family: Arial, Helvetica, sans-serif; font-size: 20px; color: black; font-weight: bold; } pw { margin-left: 20px; font-family: Arial, Helvetica, sans-serif; font-size: 40px; color: white; font-weight: bold; } pw2 { margin-left: 20px; font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: white; font-weight: bold; } pw3 { font-family: Arial; color: black; font-size: 20px; font-weight: bold; margin-left: 40px; margin-right: 40px; } pw4 { font-family: Arial; color: black; font-size: 20px; font-weight: bold; margin-left: 150px; margin-right: 0px; } pw5 { margin-left: 880px; font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: black; font-weight: bold; } pw6 { font-family: Arial; color: black; font-size: 20px; font-weight: bold; margin-left: 275px; margin-right: 0px; } a:link { color: blue; background-color: transparent; text-decoration: none; } a:visited { color: aqua; background-color: transparent; text-decoration: none; } a:hover { color: navy; background-color: transparent; font-size: 23px; } a:active { color: fuchsia; background-color: transparent; text-decoration: underline; } h5 { color: black; margin-left: 40px; text-decoration: underline; text-transform: uppercase; text-align: left; font-size: 35px; text-shadow: 2px 1px grey; } a.zoom:hover { transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large, it will go outside of the viewport) */ } </style> <div class="a"></div> margin1 { margin-left: 1cm; } 类的Deque<Card>方法对Card集合的内容进行排序。

getRealValue()

这是我的CardType枚举

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();
  }
}

,我想根据public enum CardType { CLUB("♣"), SPADE("♠"), HEART("♥"), DIAMOND("♦"); public final String icon; private CardType(String icon) { this.icon = icon; } }

对我的双端队列进行排序

1 个答案:

答案 0 :(得分:3)

您可以随时清除它,然后以正确的顺序重新插入元素:

Card[] a = deque.toArray(new Card[0]);
Arrays.sort(a);
deque.clear();
for (Card card : a) {
    deque.add(card);
}

请牢记此性能。如果您的结构需要排序,请考虑使用PriorityQueue