【8.19】包装类 & ArrayList常用操作 & 迭代器 & LinkedList链表

【8.19】包装类 & ArrayList常用操作 & 迭代器 & LinkedList链表
利用顺序表实现扑克牌的简单操作

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Random;

public class Main {
    private static void swap(ArrayList<PokerCard> list,int i,int j){
        PokerCard tmpI = list.get(i);
        PokerCard tmpJ = list.get(j);
        list.set(i,tmpJ);
        list.set(j,tmpI);
    }

    public static void main(String[] args) {
        ArrayList<PokerCard> cards = new ArrayList<>(52);
        String[] colors = {"♥","♦","♣","♠"};

        for(int i = 0; i < 4; i++){
            for(int j = 1; j <= 13; j++){
                PokerCard card = new PokerCard(j, colors[i]);
                cards.add(card);
            }
        }
        //System.out.println(cards);

        //洗牌

        /*
        Random random = new Random(20190820);
        for(int i = 0; i < 10; i++){
            System.out.println(random.nextInt(20));
        }
        */

        Random random = new Random(20190820);
        for(int i = 51; i > 0; i--){
            int j = random.nextInt(i);
            swap(cards, i, j);
        }
        System.out.println(cards);

        //有三个人,每个人抓 5 张牌
        ArrayList<PokerCard> A = new ArrayList<>();
        ArrayList<PokerCard> B = new ArrayList<>();
        ArrayList<PokerCard> C = new ArrayList<>();

        System.out.println(cards.subList(52 - 15, 52));

        for (int i = 0; i < 15; i++) {
            PokerCard card = cards.remove(cards.size() - 1);
            switch(i % 3){
                case 0:
                    A.add(card);
                    break;
                case 1:
                    B.add(card);
                    break;
                case 2:
                    C.add(card);
                    break;
            }
        }
        System.out.println(cards.size());
        System.out.println(A);
        System.out.println(B);
        System.out.println(C);

        //A抓到的牌中,有没有[红心 A]
        PokerCard heartA = new PokerCard(3,"♣");

        for(PokerCard card: A){
            //如何判断card是不是红心 A
            //自定义类如何判断两个对象相等?
            if(card.equals(heartA)){
                System.out.println("包含");
            }
        }

        if(A.contains(heartA)){
            System.out.println("包含");
        }else{
            System.out.println("不包含");
        }

        /*
        System.out.println(A);
        for(int i = 0; i < A.size(); i++){
            PokerCard o = A.get(i);
            if(o.equals(heartA)){
                A.remove(o);
            }
        }
        System.out.println(A);
        */

        //迭代器
        System.out.println(A);
        Iterator<PokerCard> it = A.iterator();
        while(it.hasNext()){
            PokerCard o = it.next();
            if(o.equals(heartA)){
                it.remove();
            }
        }
        System.out.println(A);

        /*
        System.out.println(A);
        for(PokerCard o : A){
            if(o.equals(heartA)){
                A.remove(o);
            }
        }
        System.out.println(A);
        */

        Iterator<PokerCard> it1 = A.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }

        ListIterator<PokerCard> it2 = A.listIterator();
        System.out.println(it2.next());
        System.out.println(it2.next());
        System.out.println(it2.previous());
    }
}

public class PokerCard {
    int value;
    String color;

    public PokerCard(int value, String color){
        this.value = value;
        this.color = color;
    }

    @Override
    public String toString() {
        return String.format("[%d, %s]",value,color);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }

        //返回obj对象能否被PokerCard类型的引用指向
        //obj对象的类型是不是PokerCard类型的小类型
        if(!(obj instanceof PokerCard)){
            return false;
        }

        PokerCard other = (PokerCard)obj;

        //equals方法,判断两个对象是否相等
        return this.value == other.value && this.color.equals(other.color);
    }
}

运行结果:

[[2, ♣], [8, ♣], [4, ♣], [13, ♦], [9, ♦], [6, ♠], [8, ♥], [6, ♥], [11, ♥], [2, ♠], [13, ♥], [12, ♦], [13, ♠], [9, ♣], [7, ♦], [12, ♠], [9, ♥], [3, ♦], [1, ♣], [10, ♣], [4, ♠], [7, ♥], [13, ♣], [4, ♥], [3, ♠], [11, ♠], [7, ♣], [12, ♣], [5, ♣], [10, ♥], [2, ♥], [11, ♦], [1, ♦], [8, ♦], [7, ♠], [1, ♥], [5, ♦], [1, ♠], [4, ♦], [10, ♠], [11, ♣], [6, ♦], [12, ♥], [9, ♠], [2, ♦], [6, ♣], [5, ♥], [3, ♥], [10, ♦], [5, ♠], [8, ♠], [3, ♣]]
[[1, ♠], [4, ♦], [10, ♠], [11, ♣], [6, ♦], [12, ♥], [9, ♠], [2, ♦], [6, ♣], [5, ♥], [3, ♥], [10, ♦], [5, ♠], [8, ♠], [3, ♣]]
37
[[3, ♣], [10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[[8, ♠], [3, ♥], [2, ♦], [6, ♦], [4, ♦]]
[[5, ♠], [5, ♥], [9, ♠], [11, ♣], [1, ♠]]
包含
包含
[[3, ♣], [10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[[10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[10, ♦]
[6, ♣]
[12, ♥]
[10, ♠]
[10, ♦]
[6, ♣]
[6, ♣]

LinkedList链表示例

import java.util.LinkedList;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();

        list.push("C++");
        list.push("JavaSE");
        list.push("数据");
        list.push("系统");
        System.out.println(list.pop());
        System.out.println(list.size());

        list.addLast("C");
        list.addLast("Java");
        list.addLast("数据结构");
        list.addLast("操作系统");
        System.out.println(list.peek());
        System.out.println(list.peekFirst());
        System.out.println(list.peekLast());
        System.out.println(list.size());

        System.out.println(list.poll());
        System.out.println(list.pollFirst());
        System.out.println(list.pollLast());
        System.out.println(list.size());
    }
}

运行结果:

系统
3
数据
数据
操作系统
7
数据
JavaSE
操作系统
4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值