【196天】黑马程序员27天视频学习笔记【Day18-下】

叨逼叨两句

收拾收拾~

18-11:Collection中的常见方法

  1. sort【要求比较的类实现comparable】
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("c");
        list.add("b");
        list.add("b");
        list.add("a");
        list.add("a");
        
        Collections.sort(list);
        System.out.println(list);
    
    }
}
  1. binarySearch
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");

        System.out.println(Collections.binarySearch(list, "c"));
        System.out.println(Collections.binarySearch(list, "b"));  //返回 -(插入点-1)
        
        
        
    }
}
  1. max
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");

        System.out.println(Collections.max(list));  //根据默认排序结果获取集合中的最大值
    
    }
}
  1. reverse【反转】
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        Collections.reverse(list);
        System.out.println(list);  
    
    }
}
  1. shuffle【洗牌】
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        Collections.shuffle(list);
        System.out.println(list);  
    
    }
}

18-(12-14):模拟斗地主

  1. 初版
package com.test.demo001;

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

public class Demo003 {
    public static void main(String[] args) {
        String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] color = {"红桃","黑桃","方片","梅花"};
        ArrayList<String> poker = new ArrayList<>();
        for(String s1 : num){
            for(String s2 : color){
                poker.add(s2.concat(s1));
            }
        }
        poker.add("小王");
        poker.add("大王");
        Collections.shuffle(poker);
        
        ArrayList<String> a = new ArrayList<>();
        ArrayList<String> b = new ArrayList<>();
        ArrayList<String> c = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();
        
        for(int i = 0; i < poker.size(); i++){
            if(i >= poker.size()-3){
                dipai.add(poker.get(i));
            } else if(i % 3 == 0) {
                a.add(poker.get(i));
            } else if(i % 3 == 1) {
                b.add(poker.get(i));
            } else {
                c.add(poker.get(i));
            } 
        }
        
        
        
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(dipai);
        
        
    }
}
  1. 终版
package com.test.demo001;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

public class Demo004 {
    public static void main(String[] args) {
        String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] color = {"红桃","黑桃","方片","梅花"};
        HashMap<Integer,String> hm = new HashMap<>();   //存索引和扑克牌
        ArrayList<Integer> list = new ArrayList<>(); //存索引,方便洗牌
        int index = 0;
        
        //拼接扑克牌并索引和扑克牌存储在hm中
        for(String s1 : num){
            for(String s2 : color){
                hm.put(index, s2.concat(s1));
                list.add(index);                //将0到51索引添加到集合中
                index++;
            }
        }
        hm.put(index, "小王");
        list.add(index);        //将52添加
        index++;
        hm.put(index, "大王");
        list.add(index);    //将53添加
        
        Collections.shuffle(list);
        
        TreeSet<Integer> a = new TreeSet<>();
        TreeSet<Integer> b = new TreeSet<>();
        TreeSet<Integer> c = new TreeSet<>();
        TreeSet<Integer> dipai = new TreeSet<>();
        
        for(int i = 0; i < list.size(); i++){
            if(i >= list.size() - 3){
                dipai.add(list.get(i));
            }else if(i % 3 == 0) {
                a.add(list.get(i));
            }else if(i % 3 == 1) {
                b.add(list.get(i));
            }else {
                c.add(list.get(i));
            }
        }
        
        lookPoker(hm,a,"a");
        lookPoker(hm,b,"b");
        lookPoker(hm,c,"c");
        lookPoker(hm,dipai,"dipai");
        
    }
    
    public static void lookPoker(HashMap<Integer, String> hm, TreeSet<Integer> ts, String name){
        System.out.println(name + "的牌是:");
        for(Integer i : ts){
              System.out.print(hm.get(i) + " ");
        }
        System.out.println();
    }
    
    
}

18-15:理解<? extends E>和<? super E>【再看一遍视频】

  1. public boolean addAll(Collection<? extends E> c)【集合1.addAll(集合2)要求添加进来的这个集合2中的元素,要不与集合1一样类型,要不就是集合1中元素的子类】
  2. public TreeSet(Comparator<? super E> comparator)【用比较器时,你相当于将TreeSet中的元素拿出来放入比较器中做比较,此时,你需要这个比较器能容纳你的元素,则这个比较器的泛型类型只能是TreeSet的泛型类型本身或者其父类】
package com.test.demo001;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

public class Demo006 {
    public static void main(String[] args) {
        
        //demo1();
        
        //? super E 泛型固定下边界
        TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
        ts1.add(new Student("张三",13));
        ts1.add(new Student("李四",23));
        ts1.add(new Student("王五",33));
        ts1.add(new Student("赵六",43));
        
        TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge());
        ts2.add(new BaseStudent("张三",13));
        ts2.add(new BaseStudent("李四",23));
        ts2.add(new BaseStudent("王五",33));
        ts2.add(new BaseStudent("赵六",43));
        
    }

    private static void demo1() {
        //? extends E 泛型固定上边界
        
        ArrayList<Student> list1 = new ArrayList<>();
        list1.add(new Student("张三",23));
        list1.add(new Student("李四",24));
        
        ArrayList<BaseStudent> list2 = new ArrayList<>();
        list2.add(new BaseStudent("王五",25));
        list2.add(new BaseStudent("赵六",26));
        
        list1.addAll(list2);
    }
}


//一个用于比较Student的比较器
class CompareByAge implements Comparator<Student>{

    @Override
    public int compare(Student s1, Student s2) {
        int num = s1.getAge() - s2.getAge();
        return num == 0 ? s1.getName().compareTo(s2.getName()):num;
    }
    
}

相关推荐