線性表
List(線性表)是一個介面,ArrayList(順序表)和LinkedList(鏈表)繼承了List,
List的常用方法:
| 方法簽名 | 功能描述 |
|---|---|
| add(E e) | 把e這個元素,尾插到線性表中 |
| add(int index,E e) | 把e這個元素,插入到index所在位置,同時,index之后的所有元素,必須"邏輯"后移 |
| remove(int index) | 洗掉index這個下標的元素,同時,index之后的所有元素,必須"邏輯"前移 |
| remove(Object o) | 洗掉第一個遇到的o元素(本質上是使用"equals"進行比較) |
| get(int index) | 回傳index下標處的元素 |
| set(int index,E e) | 用e替換index下標處的元素 |
| size() | 元素個數 |
| clear() | 清空線性表 |
| isEmpty() | 判斷是否為一個空的線性表 |
| contains(Object o) | 回傳線性表中,是否包含o這個物件(本質上使用的是"equals"方法) |
| indexOf(Object o) | 回傳線性表中,第一個遇到o這個物件的下標(equals) |
| lastIndexOf(Object o) | 回傳線性表中,最后一個遇到o這個物件的下標(equals) |
| subList(int from,int to) | 從線性表中,截取一段線性表下來,[from,to)不會影響原線性表 |
| sort(Comparator cmp) | 對線性表進行排序,以傳入的比較器進行元素的比較 |
| iterator() | 回傳迭代器,進行從前向后遍歷 |
| listIterator() | 效果等同于listInterator(0) |
| listIterator(int index) | 從index位置開始遍歷 |
| toArray() | List轉成陣列 |
ArrayList(順序表):
| 方法 | 解釋 |
|---|---|
| ArrayList() | 無參構造 |
| ArrayList(Collection<? extends E> c) | 利用其它Collection構建ArrayList |
| ArrayList(int initialCapacity) | 指定順序表初始容量 |
LinkedList(鏈表):
| 方法 | 解釋 |
|---|---|
| LinkedList() | 無參構造 |
不管是List、ArrayList還是LinkedList都涉及到泛型,需要什么型別的線性表就在泛型里填什么就好了,注意基本型別必須全部用包裝類,接下來我們來玩會撲克牌,
游戲一:一副撲克牌(只要各花色從1到5的牌)
- 洗牌
- 給5名玩家,各發兩張牌
- 依次展示5名玩家手中的牌
分析:
- 需要撲克牌類來定義每一張牌
- 需要List<撲克牌> 牌組 = new ArrayList<>();
- 需要玩家類,玩家類中包含List<撲克牌> 手牌
- 將牌組的牌分發給個玩家手中
撲克牌類:
public class Card {
public String suit;//花色
public int rank;//牌面值
public Card(String suit,int rank){
this.suit = suit;
this.rank = rank;
}
@Override
//列印這張牌
public String toString() {
return String.format("[%s %d]",suit,rank);
}
}
玩家類:
import java.util.ArrayList;
import java.util.List;
public class Player {
public List<Card> cards = new ArrayList<>();//手牌
public String name;//玩家姓名
public Player(String name) {
this.name = name;
}
}
玩游戲:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Game {
//制作撲克牌(初始化)
private static void initializeCards(List<Card> cards){
for(String suit: new String[]{"?","?","?","?"}){
for(int rank = 1;rank <= 5;rank++){
Card card = new Card(suit,rank);
//把撲克牌放入牌組中
cards.add(card);
}
}
}
public static void main(String[] args) {
//5名玩家,用List將其保存起來
List<Player> playerList = new ArrayList<>();
playerList.add(new Player("周潤發"));
playerList.add(new Player("劉德華"));
playerList.add(new Player("周星馳"));
playerList.add(new Player("高進"));
playerList.add(new Player("未知用戶"));
//牌組容器
List<Card> cardList = new ArrayList<>();
//初始化撲克牌
initializeCards(cardList);
System.out.println("初始化好的牌:");
System.out.println(cardList);
//洗牌,可以直接呼叫Collections.shuffle來洗牌
Collections.shuffle(cardList);
System.out.println("洗牌之后,抽排之前:");
System.out.println(cardList);
//發牌
int n = 2;//每名玩家發幾張牌
for(int i = 0; i < n;i++){ //一共發2輪牌
for(Player plaryer:playerList){ //每名玩家依次抽排
//從牌組中,抽一張牌出來
Card card = cardList.remove(0);
//把這張牌放到玩家的手牌中
plaryer.cards.add(card);
}
}
for(Player player:playerList){
System.out.printf("玩家[%s]的手牌是:%s%n",player.name,player.cards);
}
System.out.println("抽完牌之后,牌組中還有的牌:");
System.out.println(cardList);
}
}
運行結果:

游戲二:
- 將游戲一中的20張牌全部發給玩家(每人4張)
- 若哪名玩家獲得黑桃1,則該玩家獲勝
分析:
- 既然需要將牌全部發完,那么就是將剛才的發牌輪數n改成5即可,
- 發完牌之后,拿每一張牌和要尋找的牌做對比,相同則該玩家獲勝
分析1只是改個數字所以不寫了,這里寫一下分析2,首先不管怎么做,我們都需要對牌進行對比,所以免不了要給card類寫equals方法,
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return rank == card.rank && Objects.equals(suit, card.suit);
}
做法一:直接使用equals依次比較
//先定義一張需要對比的牌黑桃1
Card toFoundCard = new Card("?",1);
for(Player player : playerList){
for(Card card:player.cards){
if(toFoundCard.equals(card)){
System.out.println(player.name+"獲勝");
return;
}
}
}
做法二:使用List的indexOf方法對比,如果不等于-1則說明找到了,注:indexOf本質上也是使用equals方法
Card toFoundCard = new Card("?",1);
for (Player player : playerList) {
if(player.cards.indexOf(toFoundCard) != -1){
System.out.println(player.name+"獲勝");
}
}
做法三:使用contains方法,true說明找到,false說明沒有找到,同樣的本質上應用equals方法
for (Player player : playerList) {
if(player.cards.contains(toFoundCard)){
System.out.println(player.name+"獲勝");
}
}
運行結果:

游戲三:
- 每名玩家依次隨機抽取下家一張手牌
- 抽取結束后,手牌中有黑桃1的玩家獲勝
分析:
- 依次抽取,則需遍歷玩家線性表(playerList)
- 當前玩家抽取其下家手牌,則需記錄當前玩家和下家
- 如果玩家是playerList中的最后一個,則其下家是第一個玩家
- 隨機抽取,需要Random來生成抽取下標
- 判斷獲勝之前寫過了所以直接拿來用就好了
Random random = new Random();
//開始抽牌,每名玩家依次抽取下家一張隨機的手牌
for(int i = 0; i < playerList.size();i++){
//當前玩家
Player currentPlayer = playerList.get(i);
//下家
//playerList中的最后一名玩家需要抽第一個玩家的手牌,所以得判斷是否為最后一個玩家
Player nextPlayer = playerList.get(i != playerList.size() - 1 ? i + 1 : 0);
//要取的牌的下標,隨機生成
int toDrawIndex = random.nextInt(nextPlayer.cards.size());
//取牌
Card drawCard = nextPlayer.cards.remove(toDrawIndex);
//放入當前玩家手中
currentPlayer.cards.add(drawCard);
}
運行結果:

游戲四:
- 發哥是賭神,具有變牌能力,如果他手上沒有黑桃1,就可以把手里面的第一張牌變成黑桃1
- 交換牌之前,有機會變一次,交換牌之后,有機會變一次
分析:
- 周潤發先得判斷自己手里有沒有黑桃1,有則發功,
- 如果沒有,則需要遍歷尋找哪個玩家的哪張手牌是黑桃1
- 完成交換
- 兩次變牌其實都是一樣的程序,大家做的時候可以抽象成方法
Player faGe = playerList.get(0);
if(!faGe.cards.contains(toFoundCard)){
//發哥手里沒有黑桃1,需要發功(交換),先把第一張牌拿出來
//發哥手里的第一張牌
Card firstCard = faGe.cards.get(0);
//然后將發哥的第一個牌換成黑桃1
faGe.cards.set(0,toFoundCard);
for(int i = 1; i < playerList.size();i++){
//需要知道哪個玩家手里拿黑桃1了,并且記錄下標
int index1 = -1;
if((index1 = playerList.get(i).cards.indexOf(toFoundCard)) != -1){
//把手持黑桃1的玩家手中的黑桃1換成發哥的第一張牌
playerList.get(i).cards.set(index1, firstCard);
}
}
}
運行結果:

注:前面的游戲都是擴展,所以只寫了最重要的部分,其實重點是為了熟練應用常用方法,下面我們再來寫一個完整的游戲,我們俗稱抽王八,這里我們叫捉鬼,
游戲五:捉鬼
規則:還是這二十張牌,去掉一張1,五個人依次抓牌,直到把所有的牌都抓光,抓牌期間,一旦出現兩張牌點數相同就可以扔掉,所有人將手里的牌整理好之后,開始依次抽取下家手中的一張牌,湊成對就扔掉,一直進行,若玩家手中沒有牌了,則退出游戲,直到剩下最后一個玩家,則該玩家被捉,
分析:
- 發牌的時候,玩家自動將自己手中配對的牌直接舍棄,
- 發牌結束后,手中沒牌的玩家直接退出即可,
- 抽牌程序還是跟之前的整體思路相同,不過得注意抽完下家的牌時,如果下家手中沒有牌了,那他就退出游戲,與此同時,當前玩家得判斷自己抽完牌之后有沒有配對,配對則舍棄,并且還要判斷手中是否有牌,沒牌則退出
Card類:
注意:我們需要再寫一個equalsRank方法,來判斷是否配對了(點數相同即配對)
import java.util.Objects;
public class Card {
public String suit;//花色
public int rank;//牌面值
public Card(String suit,int rank){
this.suit = suit;
this.rank = rank;
}
@Override
//列印這張牌
public String toString() {
return String.format("[%s %d]",suit,rank);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return rank == card.rank && Objects.equals(suit, card.suit);
}
//再寫一個equals方法,使得只要點數相同即為true
public boolean equalsRank(Card card){
return rank == card.rank;
}
}
Player類:
注:重寫toString方法,利于觀察游戲程序,除錯bug,
import java.util.ArrayList;
import java.util.List;
public class Player {
public List<Card> cards = new ArrayList<>();//手牌
public String name;//玩家姓名
public Player(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player{" +
"cards=" + cards +
", name='" + name + '\'' +
'}';
}
}
Game類:
import java.util.*;
public class Game {
//玩家初始化
private static List<Player> initializePlayerList() {
List<Player> playerList = new ArrayList<>();
playerList.add(new Player("周潤發"));
playerList.add(new Player("劉德華"));
playerList.add(new Player("周星馳"));
playerList.add(new Player("高進"));
playerList.add(new Player("未知用戶"));
return playerList;
}
//撲克牌初始化,并直接刪掉黑桃1
private static List<Card> initializeCardList() {
List<Card> cardList = new ArrayList<>();
for(String suit:new String[]{"?","?","?","?"}){
for(int rank = 1; rank <= 5;rank++){
cardList.add(new Card(suit,rank));
}
}
//刪掉黑桃1
cardList.remove(0);
return cardList;
}
//將列印手牌抽象成方法,方便多次使用
private static void printHands(List<Player> playerList){
for (Player player : playerList) {
System.out.printf("%s 的手牌:%s%n",player.name,player.cards);
}
}
//將發牌抽象成方法,玩家摸牌程序中自動檢查是否配對,配對則扔掉
private static void drawCard(List<Player> playerList, List<Card> cardList) {
//只要cardList中還有牌就繼續抽
while(true) {
for (Player player : playerList) {
//if(cardList.size() == 0){//兩種方式都可以
//注意,這里特意將if判斷寫在for回圈里面,
// 這樣可以達到每個玩家都可以判斷是否還有牌
if(cardList.isEmpty()){
return;
}
Card card = cardList.remove(0);
boolean flag = true;
//練一下迭代器,判斷抽上來的牌和手牌是否配對
Iterator<Card> it = player.cards.iterator();
while(it.hasNext()){
Card everyCard = it.next();
if(everyCard.equalsRank(card)){
it.remove();
flag = false;
break;
}
}
//沒有配對成功,則插入
if(flag){
player.cards.add(card);
}
}
}
}
public static void main(String[] args) {
List<Player> playerList = initializePlayerList();
List<Card> cardList = initializeCardList();
Collections.shuffle(cardList);
//發牌
drawCard(playerList,cardList);
//列印發牌結束之后每個玩家手中的牌
printHands(playerList);
//把沒有手牌的玩家去掉
Iterator<Player> iterator = playerList.iterator();
while(iterator.hasNext()){
Player next = iterator.next();
if(next.cards.isEmpty()){
iterator.remove();
}
}
//開始游戲
//只剩一名玩家的時候就結束
Random random = new Random();
while(playerList.size() != 1){
for(int i = 0;i < playerList.size();i++){
//多判斷一次是防止出現最后一名玩家自己抽自己的情況,
if(playerList.size() == 1) break;
System.out.println("目前在游戲中的玩家有:");
printHands(playerList);
//獲得當前玩家
Player current = playerList.get(i);
int nextIndex = i == playerList.size()-1 ? 0 : i+1;
//獲得下家
Player next = playerList.get(nextIndex);
//current隨機抽取next的一張手牌
int randomIndex = random.nextInt(next.cards.size());
//先把牌抽出來
Card card = next.cards.remove(randomIndex);
System.out.println(current.name+"抽了"+next.name+"一張"+card);
//如果下家的手牌為空了,下家退出
if(next.cards.isEmpty()){
playerList.remove(nextIndex);
}
//判斷抽出來的牌有沒有和手牌中一樣的,如果有就去掉,
boolean flag = true;
Iterator<Card> it2 = current.cards.iterator();
while(it2.hasNext()){
if(it2.next().equalsRank(card)){
it2.remove();
flag = false;
break;
}
}
if(flag){
current.cards.add(card);
}
//如果當前玩家手中沒有牌則退出游戲
if(current.cards.isEmpty()){
playerList.remove(i);
i = i - 1;
}
}
}
System.out.println(playerList.get(0).name + "被捉了");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248982.html
標籤:其他
下一篇:植發咨詢日記之打磚塊
