利用TreeSet自动排序

This commit is contained in:
seahi 2024-12-05 09:38:36 +08:00
parent c99f6f67b2
commit 01d80d8ad2
2 changed files with 4 additions and 14 deletions

View File

@ -49,12 +49,7 @@ public class Game {
}
}
// 3. 整理手牌
player1.sortHand();
player2.sortHand();
player3.sortHand();
// 4. 显示结果
// 3. 显示结果
showResult();
}

View File

@ -3,11 +3,13 @@ package chapter6.demo64;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;
// Player.java - 玩家类
public class Player {
private String name;
private ArrayList<Card> hand = new ArrayList<>();
// Card类已经实现Comparable接口可以使用TreeSet自动排序
private TreeSet<Card> hand = new TreeSet<>();
/**
* 构造函数
@ -25,13 +27,6 @@ public class Player {
hand.add(card);
}
/**
* 整理手牌
*/
public void sortHand() {
Collections.sort(hand);
}
/**
* 显示手牌
* @return 手牌的字符串表示