一.概念:
- 需要清楚的几个点:
- 集合分为哪几个部分
- 说一说map和set
- 哪个集合是双链表,哪个是数组,哪个是线程安全的
- 说说哈希表
- 浅谈一下HashMap的底层原理:是哈希表,哈希表的本质也是数组
- set是有的吗?
- HashMap和HashTable的区别
- 算法类Collections中你用过哪几个方法?
- ArrayList,HashMap
二.用图归纳:
1.2
2.
3.
三.代码练习:
package com.pro.text;
import org.junit.Test;
import java.util.*;
public class ListTest {
@Test
public void testArrayList(){
//ArrayList实现了List接口
//如果用List<String>,不会报错,相当于多态
ArrayList<String> strList = new ArrayList<String>();
//增加
strList.add("aaa");
strList.add("bbb");
strList.add("ccc");
strList.add("ddd");
strList.add("eee");
System.out.println(strList.size());
//在指定位置插入数据
strList.add(1,"小星星");
System.out.println(strList);
//根据名字删除
strList.remove("ccc");
System.out.println(strList);
//根据索引删除指定元素
String remove = strList.remove(3);
System.out.println("这里看似删除了,实际上是留了后手,拿到了删除的数据:"+remove);
System.out.println(strList);
//查看集合当中是否包含要查的元素
boolean bool = strList.contains("小星星");
System.out.println("有小星星还是没有:"+bool);
}
@Test
public void testLinkedList(){
//如果用List<String>,会报错,相当于多态
LinkedList<String> strList = new LinkedList<String>();
strList.add("aaa");
strList.add("bbb");
System.out.println(strList);
strList.add(1,"ccc");
System.out.println(strList);
strList.add("111");
strList.add("eee");
System.out.println(strList);
strList.removeFirst();
System.out.println(strList);
}
@Test
public void testSet(){
HashSet<String> set = new HashSet<String>();
//添加是无序的,不能重复添加
set.add("cc");
set.add("aa");
set.add("aa");
set.add("bb");
System.out.println(set);
System.out.println(set.size());
//删除
set.remove("aa");
System.out.println(set);
}
@Test
public void testMap(){
HashMap<String, Object> map = new HashMap<String, Object>();
//添加,建值唯一,重复写会覆盖
map.put("k1","aa");
map.put("k1","bb");
map.put("k2","cc");
map.put("k3","dd");
map.put("k4","ee");
System.out.println(map+","+map.size());
Set<String> keySet = map.keySet();
//因为是无序的,所以不能用for循环遍历,而改用迭代器,因为collection继承了迭代器这个接口
/* for (int i = 0; i < keySet; i++) {
}*/
//使用迭代器遍历所有元素,打印出来-->打印出来的全部是"键",用来操作Key
Iterator<String> iterator = keySet.iterator();
/* while (iterator.hasNext()){
String next = iterator.next();
System.out.print(next+"\t");
}*/
//增强for循环,快捷键iter
for (String s : keySet) {
System.out.print(s+"\t");
}
System.out.println();
//操作值value
Collection<Object> values = map.values();
System.out.println("打印值:"+values);
//同时操作键和值
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey()+","+entry.getValue());
}
//
Set<String> sets = map.keySet();
//通过键获得值并打印出来
for (String key : sets) {
Object o = map.get(key);
System.out.println(o);
}
//删除
}
}
四.案例
- 4种花色,每一种都345678910,J,Q,K,A,,2, 三个玩家,发斗地主的牌。三个集合表示三个玩家,花色一个集合, 花色+3 ,生成54张牌。洗牌发牌 ,打印出每个人手里的牌
- 代码实现
package com.pro.text; import org.junit.Test; import java.util.ArrayList; import java.util.Collections; public class CardTest { @Test public void cardTest() { //定义⼀个集合装扑克 ArrayList<String> puKe = new ArrayList<String>(); //定义两个数组 String[] colors = {"♠", "♥", "♣", "♦"}; String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; //先把两张特殊的牌(⼤王和⼩王)使⽤add()⽅法添加到集合puKe中。如果需要⽆能牌也可以再次添加 puKe.add("⼤王"); puKe.add("⼩王"); //puKe.add("替牌"); //使⽤双重for循环把两个数组装。得到52张牌 for (int i = 0; i < colors.length; i++) { for (int j = 0; j < numbers.length; j++) { //得到数组colors和数组numbers组合 puKe.add(colors[i] + numbers[j]);//[♠2,♠3,......] } } //接下来就是第⼆步,洗牌了 //咱们可以直接使⽤Collections类中的shuffle(List<?> list)⽅法,把puKe这个装着扑克的集合作为参数传⼊该⽅法中,把扑克原来的顺序打乱; Collections.shuffle(puKe); //第三步就是发牌了 //⾸先得定义三个玩家的集合,和⼀个底牌的集合。这样才好把puKe这个集合中的牌发到三给玩家集合中 ArrayList<String> player1 = new ArrayList<String>(); ArrayList<String> player2 = new ArrayList<String>(); ArrayList<String> player3 = new ArrayList<String>(); ArrayList<String> diPai = new ArrayList<String>(); //直接使⽤for循环把集合puKe中的54张牌遍历出来 for (int i = 0; i < puKe.size(); i++) { //把遍历的每张牌赋值给p; String p = puKe.get(i); //这⾥⾸先把底牌先弄到集合diPai中,可以使⽤if判断是否是最后三张,如果是就把最后三张放到集合diPai中 if (i >= 51) { //当集合puKe的下标⼤于等于51时,也就能说明这是最后三张扑克了;然后把p添加到集合第P哎当中即可 diPai.add(p); //当集合puKe的下标取模3==0时给玩家⼀发牌 } else if (i % 3 == 0) { player1.add(p); //当集合puKe的下标取模3==1时给玩家⼀发牌 } else if (i % 3 == 1) { player2.add(p); //当集合puKe的下标取模3==2时给玩家⼀发牌 } else if (i % 3 == 2) { player3.add(p); } } //最后看牌就简单了 //直接System.out.ptintln();打印玩家集合即可, System.out.println("player1:" + player1); System.out.println("player2:" + player2); System.out.println("player3:" + player3); System.out.println("底牌:" + diPai); } }