guyanqing 10 mesi fa
parent
commit
4bfc0c1ff7

+ 0 - 1
src/main/java/com/sf/javase/day11/exception/MyExceptionTest.java

@@ -16,6 +16,5 @@ public class MyExceptionTest {
             throw new MyException("age<0",2);
 //            ArrayIndexOutOfBoundsException
         }
-
     }
 }

+ 70 - 0
src/main/java/com/sf/javase/day13/collection/T.java

@@ -0,0 +1,70 @@
+package com.sf.javase.day13.collection;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.*;
+
+/**
+ * 集合框架 - collection - ArrayList
+ */
+
+public class T {
+    @Test
+    public void t1(){
+        Collection<String> collection = new ArrayList<>();
+        collection.add("admin");
+        collection.add("jom");
+
+        System.out.println(collection);
+        Collection<String> collection2 = new ArrayList<>();
+        System.out.println(collection2);
+        collection2.add("jerry");
+        collection2.add("tom");
+        collection.addAll(collection2);
+        System.out.println(collection);
+        System.out.println(collection2.size());
+        System.out.println(collection.size());
+        System.out.println("isEmpty = "+collection.isEmpty());
+        boolean jom = collection.contains("jom1");
+        System.out.println(jom);
+        System.out.println(collection.containsAll(collection2));
+        Collection collection3 = new ArrayList();
+        collection3.add("jerry");
+        collection3.add("tom");
+        System.out.println(collection3.equals(collection2));
+        //  co   4    c2   2
+       collection.remove("admin");
+    }
+
+
+    @Test
+    public void t2(){
+        List<String> list = new ArrayList<>();
+        list.add("abc");
+        list.add("afg");
+        list.add("amf");
+        list.add("mfg");
+        list.add("age");
+        list.add("ofh");
+        // 去除包含a字母的元素     contain     remove    [mfg,ofh]
+        for (int i = 0; i < list.size(); i++) {
+            if(list.get(i).contains("a")){
+                list.remove(i);
+                i--;
+            }
+        }
+        System.out.println(list);
+
+
+        //迭代器
+        Iterator<String> iterator = list.iterator();
+        while (iterator.hasNext()){
+            String next = iterator.next();
+            if(next.contains("a")){
+                iterator.remove();
+            }
+        }
+
+        System.out.println("===="+list);
+    }
+}

+ 69 - 0
src/main/java/com/sf/javase/day13/list/Game.java

@@ -0,0 +1,69 @@
+package com.sf.javase.day13.list;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+public class Game {
+    private static final String[] suits = {"♥", "♣", "♦", "♠"};
+    // 生成牌
+    public  List<Poker> buyPoker(){
+        List<Poker> pokers  = new ArrayList<>();
+        for (int i = 0 ;i< suits.length;i++){
+            for (int j = 1 ;j <= 13 ;j++){
+                Poker poker = new Poker(suits[i], j);
+                pokers.add(poker);
+            }
+        }
+        return pokers;
+    }
+
+
+    //洗牌
+    public void shuffle(List<Poker> pokers){
+        for (int i = pokers.size()-1; i > 0; i--) {  //0-51
+            Random random = new Random();
+            int index = random.nextInt(i);
+//           int  index =  (int)(Math.random()*(i+1));
+            //交换
+            Poker poker = pokers.get(i);
+            pokers.set(i,pokers.get(index));
+            pokers.set(index,poker);
+        }
+    }
+
+    //发牌  总人数*每个人的排数   = 总排数
+
+    public List<List<Poker>> FP(List<Poker> pokers){
+        //  4个人    每人发13张
+        List<List<Poker>> persons = new ArrayList<>();
+        List<Poker> person1 = new ArrayList<>();
+        List<Poker> person2 = new ArrayList<>();
+        List<Poker> person3 = new ArrayList<>();
+        List<Poker> person4 = new ArrayList<>();
+        persons.add(person1);
+        persons.add(person2);
+        persons.add(person3);
+        persons.add(person4);
+        for (int i = 0;i<13;i++){    //牌数
+            for(int j = 0;j < 4;j++){    //人数
+                //发过的牌不能在发
+                Poker remove = pokers.remove(0);
+                persons.get(j).add(remove);
+            }
+        }
+        return persons;
+    }
+
+
+    public static void main(String[] args) {
+        Game game = new Game();
+        List<Poker> pokers = game.buyPoker();
+        System.out.println(pokers);
+        game.shuffle(pokers);
+        List<List<Poker>> fp = game.FP(pokers);
+        for (List<Poker> list : fp) {
+            System.out.println(list);
+        }
+    }
+}

+ 43 - 0
src/main/java/com/sf/javase/day13/list/Poker.java

@@ -0,0 +1,43 @@
+package com.sf.javase.day13.list;
+
+/**
+ * 扑克类
+ */
+public class Poker {
+    //   花色
+    private String suit;
+    //   数字
+    private int rank;
+
+    public Poker() {
+    }
+
+    public Poker(String suit, int rank) {
+        this.suit = suit;
+        this.rank = rank;
+    }
+
+    public String getSuit() {
+        return suit;
+    }
+
+    public void setSuit(String suit) {
+        this.suit = suit;
+    }
+
+    public int getRank() {
+        return rank;
+    }
+
+    public void setRank(int rank) {
+        this.rank = rank;
+    }
+
+    @Override
+    public String toString() {
+        return "Poker{" +
+                "suit='" + suit + '\'' +
+                ", rank=" + rank +
+                '}';
+    }
+}

+ 36 - 0
src/main/java/com/sf/javase/day13/list/T.java

@@ -0,0 +1,36 @@
+package com.sf.javase.day13.list;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * list集合
+ */
+public class T {
+    @Test
+    public void t1(){
+        List<String> list = new ArrayList();
+        list.add("admin");
+        list.add(1,"tom");
+        list.add("admin2");
+        list.add("admin3");
+        list.add("admin4");
+        list.add("admin5");
+        list.add(5,"tom");
+        list.set(5,"tom1");
+        System.out.println(list);
+//        list.subList()
+    }
+
+    @Test
+    public void t2(){
+        List<String> list = new ArrayList<>(8);
+        for (int i = 5; i > 0; i--) {
+            list.add("第"+i+"个~");
+        }
+        List<String> list1 = new ArrayList<>(list);
+        System.out.println(list1);
+    }
+}

BIN
target/classes/com/sf/javase/day11/exception/MyExceptionTest.class


BIN
target/classes/com/sf/javase/day12/comparable/Goods.class


BIN
target/classes/com/sf/javase/day12/comparable/GoodsTest$1.class


BIN
target/classes/com/sf/javase/day12/comparable/GoodsTest.class


BIN
target/classes/com/sf/javase/day12/comparable/Student.class


BIN
target/classes/com/sf/javase/day13/collection/T.class


BIN
target/classes/com/sf/javase/day13/list/Game.class


BIN
target/classes/com/sf/javase/day13/list/Poker.class


BIN
target/classes/com/sf/javase/day13/list/T.class