【Java学习】斗地主案例(单列)、数据结构、List集合

斗地主案例

在这里插入图片描述

import java.util.ArrayList;
import java.util.Collections;

public class DouDiZhu {

    public static void main(String[] args) {
        // 1. 准备牌
        ArrayList<String> poke = new ArrayList<>();
        // 定义两个数组,存放花色和牌号
        String[] colors = {"♠","♥","♣","♦"};
        String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        poke.add("大王");
        poke.add("小王");
        // 循环嵌套,组装52张牌
        for (String number : numbers) {
            for (String color : colors) {
                // 将牌放入poke集合中
                poke.add(color + number);
            }
        }
        // 2. 洗牌
        //         使用集合的工具类Collections中的方法
        //          Static void shuffle(List<?> list)
        // 使用默认随机源对指定列表进行置换
        Collections.shuffle(poke);

        // 3.发牌
        ArrayList<String> play01 = new ArrayList<>();
        ArrayList<String> play02 = new ArrayList<>();
        ArrayList<String> play03 = new ArrayList<>();
        ArrayList<String> diPai = new ArrayList<>();
        // 遍历poke集合
        // 使用poke索引%3给三个玩家发牌,剩余三张牌给底牌
        for (int i = 0; i < poke.size(); i++) {
            // 获取每一种牌
            String p = poke.get(i);
            if (i >= 51) {
                diPai.add(p);
            } else if (i % 3 == 0) {
                play01.add(p);
            }else if (i % 3 == 1) {
                play02.add(p);
            }else if (i % 3 == 2) {
                play03.add(p);
            }
        }
        System.out.println("刘德华:" + play01);
        System.out.println("周润发:" + play02);
        System.out.println("周星驰:" + play03);
        System.out.println("底牌" + diPai);
    }
}

数据结构

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

List集合

List接口 extends Collection接口
List接口的特点:

1.有序的集合,存储元素和取出元素的顺序是一致的
2.有索引,包含了一些带索引的方法
3.允许存储重要的元素

特有的方法:

public void add(int index, E element): 将指定的元素,添加到集合中的指定位置上
public E get(int index): 返回集合中指定位置的元素
public E remove(int index): 移除列表中指定位置的元素,返回的是被移除的元素
publicE set(int index, E element): 用指定元素替换集合中指定位置的元素,返回值是更新前的元素

注意:
== 操作索引的时候,一定要防止索引越界异常==

List集合的遍历常用方法

		// 1.普通for循环遍历
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }

        // 2.迭代器
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }

        // 3.增强for循环
        for (String s : list) {
            System.out.println(s);
        }
        

LinkedList集合的特点:
1.底层是一个链表结构:查询慢,增删快
2.里面包含了大量操作首尾元素的方法
注意:使用LinkedList集合特有的方法,不能使用多态

public void addFirst(E e): 将指定元素插入此列表的开头。
public void addLast(E e): 将指定元素插入此列表的结尾。
public void push(E e): 将此元素推入此列表所表示的堆栈。

public E getFirst(): 返回此列表的第一个元素
public E getLast(): 返回此列表的最后一个元素

public E removeFirst():移除并返回此列表的第一个元素
public E removeLast(): 移除并返回此列表的最后一个元素
public E pop(): 从此列表所表示的堆栈处弹出一个元素

public boolean isEmpty(): 如果列表不包含元素,则返回true
发布了19 篇原创文章 · 获赞 20 · 访问量 724

猜你喜欢

转载自blog.csdn.net/sy140823/article/details/104804709