Java 复习整理day07
- package com.it.demo05_innerclass;
- /*
- 案例: 演示内部类入门.
- 概述:
- 所谓的内部类指的是类里边还有一个类, 里边那个类叫: 内部类, 外边那个类, 叫外部类.
- 分类:
- 成员内部类:
- 定义在成员位置的内部类.
- 局部内部类:
- 定义在局部位置的内部类.
- */
- public class A { //外部类
- //成员变量
- public int age = 10;
- //成员内部类
- public class B {
- }
- //成员方法
- public void show() {
- //局部变量
- int age = 10;
- //局部内部类
- class B {
- }
- }
- }
- package com.it.demo05_innerclass;
- //自定义的抽象类, 表示动物类
- public abstract class Animal {
- //抽象方法, 表示: 吃
- public abstract void eat();
- }
- package com.it.demo05_innerclass;
- //子类, 猫类
- public class Cat extends Animal {
- @Override
- public void eat() {
- System.out.println("猫吃鱼!");
- }
- }
- package com.it.demo05_innerclass;
- /*
- 案例: 匿名内部类入门
- 需求:
- 1.定义Animal抽象类, 该类中有一个抽象方法eat().
- 2.在测试类的main方法中, 通过匿名内部类的形式创建Animal抽象类的子类对象.
- 3.调用Animal类中的eat()方法
- 匿名内部类简介:
- 概述:
- 匿名内部类指的是没有名字的局部内部类.
- 格式:
- new 类名或者接口名(){
- //重写类或者接口中所有的抽象方法
- };
- 前提条件:
- 必须有一个类或者接口.
- 本质:
- 专业版解释: 匿名内部类就是一个继承了类或者实现了接口的匿名的子类对象.
- 大白话翻译: 匿名内部类不是类, 而是一个 子类对象.
- 应用场景:
- 1. 当对 对象方法(也叫成员方法)仅调用一次的时候.
- 2. 可以作为方法的实参进行传递.
- */
- public class AnimalTest {
- public static void main(String[] args) {
- //需求: 调用Animal#eat()
- //方式一: 普通调用.
- //创建一个 继承了Animal类的子类的对象, 子类名叫: Cat, 对象名叫: an
- Animal an = new Cat();
- an.eat();
- //方式二: 匿名对象实现.
- //创建一个 继承了Animal类的子类的对象, 子类名叫: Cat, 对象名叫: 不知道(匿名)
- new Cat().eat();
- //方式三: 匿名内部类的方式实现.
- //创建一个 继承了Animal类的子类的对象, 子类名叫: 不知道, 对象名叫: 不知道(匿名)
- new Animal(){
- //重写类或者接口中所有的抽象方法
- @Override
- public void eat() {
- System.out.println("我是匿名内部类, 爱吃啥吃啥!");
- }
- }.eat();
- }
- }
- package com.it.demo05_innerclass;
- /*
- 案例: 演示匿名内部类的应用场景.
- 应用场景:
- 1. 当对 对象方法(也叫成员方法)仅调用一次的时候.
- 2. 可以作为方法的实参进行传递.
- 关于匿名内部类使用时的一个小技巧:
- 一般是抽象类或者接口中的抽象方法不超过3个的时候(绝大多数是1个), 就可以考虑使用匿名内部类了.
- */
- public class AnimalTest02 {
- public static void main(String[] args) {
- //演示使用场景1: 当对 对象方法(也叫成员方法)仅调用一次的时候.
- //如果多次调用, 复杂写法如下
- //method01();
- //简化版: 通过多态实现.
- Animal an = new Animal(){ //父类引用指向子类对象.
- @Override
- public void eat() {
- System.out.println("匿名内部类, 随便吃点啥!");
- }
- };
- an.eat();
- an.eat();
- an.eat();
- System.out.println("--------------------------");
- //演示匿名内部类的使用场景2: 可以作为方法的实参进行传递.
- //printAnimal(这里要的是Animal类的子类对象);
- //printAnimal(an); //简写版
- //标准版
- //printAnimal(new Cat());
- printAnimal(new Animal(){ //匿名内部类的本质就是: 一个子类对象
- @Override
- public void eat() {
- System.out.println("匿名内部类, 可以作为方法的实参进行传递!");
- }
- });
- }
- //定义方法, 接收Animal对象, 调用eat()方法
- public static void printAnimal(Animal an) {
- an.eat();
- }
- //演示匿名内部类的重复调用.
- public static void method01() {
- new Animal(){
- @Override
- public void eat() {
- System.out.println("匿名内部类, 随便吃点啥!");
- }
- }.eat();
- //重复调用
- new Animal(){
- @Override
- public void eat() {
- System.out.println("匿名内部类, 随便吃点啥!");
- }
- }.eat();
- }
- }
- package cn.it.demo01;
- /*
- * 实现正则规则和字符串进行匹配,使用到字符串类的方法
- * String类三个和正则表达式相关的方法
- * boolean matches(String 正则的规则)
- * "abc".matches("[a]") 匹配成功返回true
- *
- * String[] split(String 正则的规则)
- * "abc".split("a") 使用规则将字符串进行切割
- *
- * String replaceAll( String 正则规则,String 字符串)
- * "abc0123".repalceAll("[\\d]","#")
- * 安装正则的规则,替换字符串
- */
- public class RegexDemo {
- public static void main(String[] args) {
- checkTel();
- }
- /*
- * 检查手机号码是否合法
- * 1开头 可以是34578 0-9 位数固定11位
- */
- public static void checkTel(){
- String telNumber = "1335128005";
- //String类的方法matches
- boolean b = telNumber.matches("1[34857][\\d]{9}");
- System.out.println(b);
- }
- /*
- * 检查QQ号码是否合法
- * 0不能开头,全数字, 位数5,10位
- * 123456
- * \\d \\D匹配不是数字
- */
- public static void checkQQ(){
- String QQ = "123456";
- //检查QQ号码和规则是否匹配,String类的方法matches
- boolean b = QQ.matches("[1-9][\\d]{4,9}");
- System.out.println(b);
- }
- }
- package cn.it.demo01;
- public class RegexDemo1 {
- public static void main(String[] args) {
- replaceAll_1();
- }
- /*
- * "Hello12345World6789012"将所有数字替换掉
- * String类方法replaceAll(正则规则,替换后的新字符串)
- */
- public static void replaceAll_1(){
- String str = "Hello12345World6789012";
- str = str.replaceAll("[\\d]+", "#");
- System.out.println(str);
- }
- /*
- * String类方法split对字符串进行切割
- * 192.168.105.27 按照 点切割字符串
- */
- public static void split_3(){
- String ip = "192.168.105.27";
- String[] strArr = ip.split("\\.");
- System.out.println("数组的长度"+strArr.length);
- for(int i = 0 ; i < strArr.length ; i++){
- System.out.println(strArr[i]);
- }
- }
- /*
- * String类方法split对字符串进行切割
- * 18 22 40 65 按照空格切割字符串
- */
- public static void split_2(){
- String str = "18 22 40 65";
- String[] strArr = str.split(" +");
- System.out.println("数组的长度"+strArr.length);
- for(int i = 0 ; i < strArr.length ; i++){
- System.out.println(strArr[i]);
- }
- }
- /*
- * String类方法split对字符串进行切割
- * 12-25-36-98 按照-对字符串进行切割
- */
- public static void split_1(){
- String str = "12-25-36-98";
- //按照-对字符串进行切割,String类方法split
- String[] strArr = str.split("-");
- System.out.println("数组的长度"+strArr.length);
- for(int i = 0 ; i < strArr.length ; i++){
- System.out.println(strArr[i]);
- }
- }
- }
- package cn.it.demo01;
- public class RegexDemo2 {
- public static void main(String[] args) {
- checkMail();
- }
- /*
- * 检查邮件地址是否合法
- * 规则:
- * 1234567@qq.com
- * mym_ail@sina.com
- * nimail@163.com
- * wodemail@yahoo.com.cn
- *
- * @: 前 数字字母_ 个数不能少于1个
- * @: 后 数字字母 个数不能少于1个
- * .: 后面 字母
- *
- */
- public static void checkMail(){
- String email ="abc123@sina.com";
- boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
- System.out.println(b);
- }
- }
- package com.it.demo06_collection;
- import java.util.ArrayList;
- import java.util.Collection;
- /*
- 案例: Collection集合入门.
- 需求:
- 1.创建Collection集合对象, 用来存储字符串.
- 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
- "hello", "world", "java"
- 3.通过输出语句, 直接打印集合对象, 并观察结果.
- 涉及到的知识点:
- 泛型:
- 概述:
- 泛指某种特定的数据类型.
- 格式:
- <数据类型>
- 细节:
- 1. 实际开发中, 泛型一般是只和集合相结合使用的, 泛型是用来约束集合中存储元素的数据类型的.
- 例如:
- Collection<String> 说明集合中只能添加 字符串.
- Collection<Integer> 说明集合中只能添加 整数.
- 2. 泛型只能是引用类型.
- 3. 前后泛型必须保持一致, 或者后边的泛型可以省略不写(这个是JDK1.7的特性: 叫菱形泛型)
- 4. 泛型一般用字母E, T, K, V表示:
- E: Element(元素), T: Type(类型), K: Key(键), V: Value(值)
- Collection接口中的成员方法:
- public boolean add(T t); 往集合中添加元素.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建Collection集合对象.
- //Collection list = new Collection(); //报错: Collection是接口, 不能直接new.
- //多态方式实现
- Collection<String> list = new ArrayList<>();
- //2. 往集合中添加元素.
- //list.add(10); //报错, 原因是因为: 集合的泛型是String, 说明该集合中只能添加字符串.
- list.add("hello");
- list.add("world");
- list.add("java");
- //3. 打印集合对象.
- System.out.println(list);
- }
- }
- package com.it.demo06_collection;
- import java.util.ArrayList;
- import java.util.Collection;
- /*
- 案例: 演示Collection集合中的成员方法:
- 涉及到的Collection接口中的成员方法:
- public boolean add(E e) 添加元素.
- public boolean remove(Object obj) 从集合中移除指定的元素.
- public void clear() 清空集合对象
- public boolean contains(Object obj) 判断集合中是否包含指定的元素
- public boolean isEmpty() 判断集合是否为空
- public int size() 获取集合的长度, 即集合中元素的个数
- 需求:
- 1.通过多态的形式, 创建Collection集合对象.
- 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
- "hello", "world", "java"
- 3.分别测试上述的6个成员方法.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合对象. 多态形式创建.
- Collection<String> list = new ArrayList<>();
- //2. 往集合中添加三个元素.
- System.out.println(list.add("hello")); //返回值是true: 因为ArrayList的特点是: 可重复.
- //System.out.println(list.add("hello")); //返回值是true: 因为ArrayList的特点是: 可重复.
- list.add("world");
- list.add("java");
- System.out.println("-----------------------");
- //3. 测试Collection集合中的成员方法
- //测试 public boolean remove(Object obj) 从集合中移除指定的元素, 存在就删除并返回true, 不存在就返回false
- /*System.out.println(list.remove("world")); //true
- System.out.println(list.remove("world123")); //false*/
- System.out.println("-----------------------");
- //测试public void clear() 清空集合对象
- //list.clear();
- System.out.println("-----------------------");
- //测试 public boolean contains(Object obj) 判断集合中是否包含指定的元素
- System.out.println(list.contains("world")); //true
- System.out.println(list.contains("world123")); //false
- System.out.println("-----------------------");
- //测试 public boolean isEmpty(), 判断集合是否为空, 当且仅当集合中元素长度为0的时候, 返回true.
- //System.out.println(list.isEmpty()); //false, 说明集合有数据.
- //测试 public int size() 获取集合的长度, 即集合中元素的个数
- System.out.println("集合的长度为: " + list.size());
- //4. 打印集合对象
- System.out.println(list);
- }
- }
- package com.it.demo06_collection;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- /*
- 案例: 演示遍历Collection集合.
- 需求:
- 1.通过多态的形式, 创建Collection集合对象.
- 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
- "hello", "world", "java"
- 3.通过迭代器遍历集合, 获取到每一个元素, 并打印到控制台上.
- 涉及到的成员方法:
- Collection接口中成员方法:
- public boolean add(E e); //添加元素到集合中.
- public Iterator<E> iterator(); //根据集合对象, 获取其对应的迭代器对象.
- 迭代器(Iterator)中的成员方法:
- public boolean hasNext(); //判断迭代器中是否有下一个元素.
- public E next(); //有就获取该元素.
- 结论(记忆): 使用集合时的步骤(4大步, 3小步)
- 1. 创建集合对象.
- 2. 创建元素对象.
- 3. 把元素对象添加到集合中.
- 4. 遍历集合.
- A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
- B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
- C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
- */
- public class Demo03 {
- public static void main(String[] args) {
- //1. 创建Collection集合对象.
- Collection<String> list = new ArrayList<>(); //list集合对象相当于: 仓库
- //2. 把字符串对象添加到Collection集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //3. 根据集合对象, 获取其对应的迭代器对象.
- Iterator<String> it = list.iterator(); //it: 迭代器对象, 相当于仓库管理员.
- //4. 判断迭代器中是否有下一个元素,
- while (it.hasNext()) {
- //s: 就是集合中的每一个元素
- //如果有就通过next()方法获取数据
- String s = it.next();
- //5. 将获取获取到的数据, 打印到控制台上.
- System.out.println(s);
- }
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- import java.util.Collection;
- /*
- * Collection接口中的方法
- * 是集合中所有实现类必须拥有的方法
- * 使用Collection接口的实现类,程序的演示
- * ArrayList implements List
- * List extends Collection
- * 方法的执行,都是实现的重写
- */
- public class CollectionDemo {
- public static void main(String[] args) {
- function_3();
- }
- /*
- * Collection接口方法
- * boolean remove(Object o)移除集合中指定的元素
- */
- private static void function_3(){
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc");
- coll.add("money");
- coll.add("itcast");
- coll.add("itheima");
- coll.add("money");
- coll.add("123");
- System.out.println(coll);
- boolean b = coll.remove("money");
- System.out.println(b);
- System.out.println(coll);
- }
- /* Collection接口方法
- * Object[] toArray() 集合中的元素,转成一个数组中的元素, 集合转成数组
- * 返回是一个存储对象的数组, 数组存储的数据类型是Object
- */
- private static void function_2() {
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc");
- coll.add("itcast");
- coll.add("itheima");
- coll.add("money");
- coll.add("123");
- Object[] objs = coll.toArray();
- for(int i = 0 ; i < objs.length ; i++){
- System.out.println(objs[i]);
- }
- }
- /*
- * 学习Java中三种长度表现形式
- * 数组.length 属性 返回值 int
- * 字符串.length() 方法,返回值int
- * 集合.size()方法, 返回值int
- */
- /*
- * Collection接口方法
- * boolean contains(Object o) 判断对象是否存在于集合中,对象存在返回true
- * 方法参数是Object类型
- */
- private static void function_1() {
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc");
- coll.add("itcast");
- coll.add("itheima");
- coll.add("money");
- coll.add("123");
- boolean b = coll.contains("itcast");
- System.out.println(b);
- }
- /*
- * Collection接口的方法
- * void clear() 清空集合中的所有元素
- * 集合容器本身依然存在
- */
- public static void function(){
- //接口多态的方式调用
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc");
- coll.add("bcd");
- System.out.println(coll);
- coll.clear();
- System.out.println(coll);
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- public class CollectionDemo1 {
- public static void main(String[] args) {
- //集合可以存储任意类型的对象
- //集合中,不指定存储的数据类型, 集合什么都存
- Collection coll = new ArrayList();
- coll.add("abc");
- coll.add("uyjgtfd");
- //迭代器获取
- Iterator it = coll.iterator();
- while(it.hasNext()){
- //it.next()获取出来的是什么数据类型,Object类
- //Object obj = it.next();
- //System.out.println(obj);
- String s = (String)it.next();
- System.out.println(s.length());
- }
- }
- }
- package cn.it.demo;
- public class Person {
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- /*public String toString() {
- return "Person [name=" + name + ", age=" + age + "]";
- }*/
- public String toString(){
- return name+"..."+age;
- }
- public Person(){}
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- /*
- * 集合体系,
- * 目标 集合本身是一个存储的容器:
- * 必须使用集合存储对象
- * 遍历集合,取出对象
- * 集合自己的特性
- */
- public class ArrayListDemo {
- public static void main(String[] args) {
- /*
- * 集合ArrayList,存储int类型数
- * 集合本身不接受基本类,自动装箱存储
- */
- ArrayList<Integer> array = new ArrayList<Integer>();
- array.add(11);
- array.add(12);
- array.add(13);
- array.add(14);
- array.add(15);
- for(int i = 0 ; i < array.size() ;i++){
- System.out.println(array.get(i));
- }
- /*
- * 集合存储自定义的Person类的对象
- */
- ArrayList<Person> arrayPer = new ArrayList<Person>();
- arrayPer.add(new Person("a",20));
- arrayPer.add(new Person("b",18));
- arrayPer.add(new Person("c",22));
- for(int i = 0 ; i < arrayPer.size();i++){
- //get(0),取出的对象Person对象
- //打印的是一个对象,必须调用的toString()
- System.out.println(arrayPer.get(i));
- }
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- /*
- * 集合中的迭代器:
- * 获取集合中元素方式
- * 接口 Iterator : 两个抽象方法
- * boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true
- * next() 取出集合中的下一个元素
- *
- * Iterator接口,找实现类.
- * Collection接口定义方法
- * Iterator iterator()
- * ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
- * 使用ArrayList集合的对象
- * Iterator it = array.iterator(),运行结果就是Iterator接口的实现类的对象
- * it是接口的实现类对象,调用方法 hasNext 和 next 集合元素迭代
- */
- public class IteratorDemo {
- public static void main(String[] args) {
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc1");
- coll.add("abc2");
- coll.add("abc3");
- coll.add("abc4");
- //迭代器,对集合ArrayList中的元素进行取出
- //调用集合的方法iterator()获取出,Iterator接口的实现类的对象
- Iterator<String> it = coll.iterator();
- //接口实现类对象,调用方法hasNext()判断集合中是否有元素
- //boolean b = it.hasNext();
- //System.out.println(b);
- //接口的实现类对象,调用方法next()取出集合中的元素
- //String s = it.next();
- //System.out.println(s);
- //迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
- while(it.hasNext()){
- String s = it.next();
- System.out.println(s);
- }
- /*for (Iterator<String> it2 = coll.iterator(); it2.hasNext(); ) {
- System.out.println(it2.next());
- }*/
- }
- }
- package cn.it.demo2;
- import java.util.ArrayList;
- /*
- * JDK1.5新特性,增强for循环
- * JDK1.5版本后,出现新的接口 java.lang.Iterable
- * Collection开是继承Iterable
- * Iterable作用,实现增强for循环
- *
- * 格式:
- * for( 数据类型 变量名 : 数组或者集合 ){
- * sop(变量);
- * }
- */
- import cn.itcast.demo.Person;
- public class ForEachDemo {
- public static void main(String[] args) {
- function_2();
- }
- /*
- * 增强for循环遍历集合
- * 存储自定义Person类型
- */
- public static void function_2(){
- ArrayList<Person> array = new ArrayList<Person>();
- array.add(new Person("a",20));
- array.add(new Person("b",10));
- for(Person p : array){
- System.out.println(p);
- }
- }
- public static void function_1(){
- //for对于对象数组遍历的时候,能否调用对象的方法呢
- String[] str = {"abc","itcast","cn"};
- for(String s : str){
- System.out.println(s.length());
- }
- }
- /*
- * 实现for循环,遍历数组
- * 好处: 代码少了,方便对容器遍历
- * 弊端: 没有索引,不能操作容器里面的元素
- */
- public static void function(){
- int[] arr = {3,1,9,0};
- for(int i : arr){
- System.out.println(i+1);
- }
- System.out.println(arr[0]);
- }
- }
- package com.it.demo07_list;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /*
- 案例; List集合入门.
- 需求:
- 1.创建List集合, 用来存储字符串.
- 2.往List集合中添加4个字符串值, 分别是: "hello", "world", "java", "world"
- 3.通过迭代器, 遍历List集合, 获取每一个元素, 并打印到控制台上.
- List集合简介:
- 概述:
- List是一个接口, 也是Collection接口的子接口, 它表示有序集合(也叫: 序列).
- 特点:
- 有序(元素的存,取顺序一致), 可重复, 元素有索引.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //分解版
- /*String s1 = "hello";
- list.add(s1);*/
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("world");
- list.add("java");
- //4. 遍历集合.
- //4.1 根据集合对象获取其对应的迭代器对象. Collection#iterator()
- Iterator<String> it = list.iterator();
- //4.2 判断迭代器中是否有下一个元素. Iterator#hasNext()
- while (it.hasNext()) {
- String s = it.next();
- //4.3 如果有, 则获取下一个元素. Iterator#next();
- System.out.println(s);
- }
- }
- }
- package com.it.demo07_list;
- import java.util.ArrayList;
- import java.util.List;
- /*
- 案例: 演示List接口的独有成员方法, 主要都是针对于 索引 操作的.
- 涉及到的List集合中的成员方法:
- public void add(int index, E element); 往指定索引处插入指定的元素.
- public E remove(int index); 根据索引, 移除其对应的数据, 并返回该数据.
- public E set(int index, E element); 修改指定索引处的元素为指定的值.
- public E get(int index); 根据索引, 获取其对应的元素.
- 细节:
- 上述四个方法的相同点是, 如果索引越界会报: 索引越界异常(IndexOutOfBoundsException).
- 需求:
- 1.创建List集合, 用来存储字符串.
- 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
- 3.演示上述的4个方法.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4. 测试上述的4个成员方法
- //测试: public void add(int index, E element); 往指定索引处插入指定的元素.
- //list.add(2, "hadoop"); //hello world hadoop java
- //list.add(3, "hadoop"); //hello world java hadoop
- //list.add(4, "hadoop"); //报错, 索引越界异常.
- //测试: public E remove(int index); 根据索引, 移除其对应的数据, 并返回该数据.
- //System.out.println(list.remove(1)); //world
- //System.out.println(list.remove(10)); //报错, 索引越界异常.
- //测试: public E set(int index, E element); 修改指定索引处的元素为指定的值.
- //System.out.println(list.set(1, "hadoop")); //world
- //System.out.println(list.set(3, "hadoop")); //报错, 索引越界异常.
- //测试: public E get(int index); 根据索引, 获取其对应的元素.
- System.out.println(list.get(0));
- System.out.println(list.get(1));
- System.out.println(list.get(2));
- //5. 打印集合.
- System.out.println(list);
- }
- }
- package com.it.demo07_list;
- import java.util.ArrayList;
- import java.util.List;
- /*
- 案例: 演示泛型类, 内容能看懂就行.
- */
- public class Demo03<HG> {
- public static void main(String[] args) {
- //需求: 调用Demo03#show();
- //泛型类: 在创建对象的时候, 明确具体的泛型.
- Demo03<String> d1 = new Demo03<>();
- d1.show("abc");
- //d1.show(10); //报错.
- Demo03<Integer> d2 = new Demo03<>();
- d2.show(10);
- //d2.show("abc"); //报错
- List<Integer> list = new ArrayList<>();
- list.add(10);
- //list.add("abc"); //报错
- }
- public void show(HG e) {
- System.out.println(e);
- }
- }
- package com.it.demo07_list;
- import java.util.ArrayList;
- import java.util.List;
- /*
- 案例: 演示List集合独有的遍历方式.
- 需求:
- 1.创建List集合, 用来存储字符串.
- 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
- 3.通过for循环 + size() + get()的形式, 遍历List集合.
- */
- public class Demo04 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4. 遍历集合. 普通for方式.
- for (int i = 0; i < list.size(); i++) {
- //根据索引, 获取集合中的元素.
- String s = list.get(i);
- System.out.println(s);
- }
- System.out.println("--------------------------");
- //通过普通for循环, 遍历List集合的快捷键: Iterator Array: itar(遍历数组), Iterator List: itli(遍历集合)
- for (int i = 0; i < list.size(); i++) {
- String s = list.get(i);
- System.out.println(s);
- }
- }
- }
- package com.it.pojo;
- //自定义的JavaBean类, 表示学生
- public class Student {
- //属性: 姓名和年龄
- private String name;
- private int age;
- public Student() {
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
- package com.it.demo04_exercise;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- /*
- 案例: 演示Collection集合存储 自定义对象并遍历.
- 需求:
- 1.定义一个学生类, 属性为姓名和年龄.
- 2.创建Collection集合, 用来存储学生对象.
- 3.往Collection集合中, 添加3个学生的信息.
- 4.通过迭代器, 遍历集合.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- Collection<Student> list = new ArrayList<>();
- //2. 创建元素对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("赵丽颖", 31);
- Student s3 = new Student("高圆圆", 35);
- Student s4 = new Student("王丽坤", 29);
- //3. 把元素对象添加到集合中.
- list.add(s1);
- list.add(s2);
- list.add(s3);
- list.add(s4);
- //4. 遍历集合.
- //4.1 根据集合对象获取其对应的迭代器对象.
- Iterator<Student> it = list.iterator();
- //4.2 判断迭代器中是否有下一个元素.
- while (it.hasNext()) {
- //4.3 如果有, 就获取下一个元素, 并输出.
- Student stu = it.next();
- System.out.println(stu);
- }
- }
- }
- package com.it.demo04_exercise;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
- /*
- 案例: 演示List集合存储自定义对象并遍历.
- 需求:
- 1.定义一个学生类, 属性为姓名和年龄.
- 2.创建List集合, 用来存储学生对象.
- 3.往List集合中, 添加3个学生的信息.
- 4.分别通过两种遍历方式, 来遍历List集合.
- 演示List集合的两种遍历方式:
- 方式一: 迭代器.
- 方式二: List体系独有的(普通for + get() + size())
- */
- public class Demo03 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<Student> list = new ArrayList<>();
- //2. 创建元素对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("赵丽颖", 31);
- Student s3 = new Student("高圆圆", 35);
- Student s4 = new Student("王丽坤", 29);
- //3. 把元素对象添加到集合中.
- list.add(s1);
- list.add(s2);
- list.add(s3);
- list.add(s4);
- //4. 遍历集合.
- //方式一: 普通迭代器.
- Iterator<Student> it = list.iterator();
- while (it.hasNext()) {
- //分解版
- /*Student stu = it.next();
- System.out.println(stu);*/
- //合并版, 正确的写法
- System.out.println(it.next());
- //合并版, 错误写法, 只能: hasNext()判断一次, next()获取一次.
- //System.out.println(it.next().getName() + "..." + it.next().getAge()); //刘亦菲...33
- }
- System.out.println("-------------------");
- //方式二: 普通for循环.
- for (int i = 0; i < list.size(); i++) {
- Student stu = list.get(i);
- System.out.println(stu);
- }
- }
- }
- package cn.it.demo3;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- /*
- * JDK1.5 出现新的安全机制,保证程序的安全性
- * 泛型: 指明了集合中存储数据的类型 <数据类型>
- */
- public class GenericDemo {
- public static void main(String[] args) {
- function();
- }
- public static void function(){
- Collection<String> coll = new ArrayList<String>();
- coll.add("abc");
- coll.add("rtyg");
- coll.add("43rt5yhju");
- // coll.add(1);
- Iterator<String> it = coll.iterator();
- while(it.hasNext()){
- String s = it.next();
- System.out.println(s.length());
- }
- }
- }
- package cn.it.demo3;
- import java.util.ArrayList;
- import java.util.Iterator;
- /*
- * 带有泛型的类
- * ArrayList
- * E: Element 元素, 实际思想就是一个变量而已
- * ArrayList<Integer> , E 接受到类型,就是Integer类型
- * public class ArrayList<E>{
- *
- * public boolean add(Integer e){
- * elementData[size++] = e;
- * }
- *
- * public boolean add(E e){}
- * }
- *
- * Iterator<E>
- * E next()
- *
- * Iterator<Integer>
- * Integer next()
- *
- */
- public class GenericDemo1 {
- public static void main(String[] args) {
- ArrayList<Integer> array = new ArrayList<Integer> ();
- array.add(123);
- array.add(456);
- // ArrayList集合,自己有个方法
- // <T> T[] toArray(T[] a)
- Integer[] i = new Integer[array.size()];
- Integer [] j = array.toArray(i);
- for(Integer k : j){
- System.out.println(k);
- }
- }
- }
- package cn.it.demo3;
- /*
- * 带有泛型的接口
- *
- * public interface List <E>{
- * abstract boolean add(E e);
- * }
- * 实现类,先实现接口,不理会泛型
- * public class ArrayList<E> implements List<E>{
- * }
- * 调用者 : new ArrayList<String>() 后期创建集合对象的时候,指定数据类型
- *
- * 实现类,实现接口的同时,也指定了数据类型
- * public class XXX implements List<String>{
- * }
- * new XXX()
- */
- public class GenericDemo2 {
- }
- package cn.it.demo4;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Iterator;
- /*
- * 泛型的通配符
- */
- public class GenericDemo {
- public static void main(String[] args) {
- ArrayList<String> array = new ArrayList<String>();
- HashSet<Integer> set = new HashSet<Integer>();
- array.add("123");
- array.add("456");
- set.add(789);
- set.add(890);
- iterator(array);
- iterator(set);
- }
- /*
- * 定义方法,可以同时迭代2个集合
- * 参数: 怎么实现 , 不能写ArrayList,也不能写HashSet
- * 参数: 或者共同实现的接口
- * 泛型的通配,匹配所有的数据类型 ?
- */
- public static void iterator(Collection<?> coll){
- Iterator<?> it = coll.iterator();
- while(it.hasNext()){
- //it.next()获取的对象,什么类型
- System.out.println(it.next());
- }
- }
- }
- package cn.it.hotel;
- /*
- * 酒店的VIP服务
- * 厨师和服务员
- */
- public interface VIP {
- public abstract void services();
- }
- package cn.it.hotel;
- /*
- * 酒店的员工类
- * 员工共性, 姓名,工号 工作方法
- */
- public abstract class Employee {
- private String name;
- private String id;
- public Employee(){}
- public Employee(String name,String id){
- this.name = name;
- this.id = id;
- }
- public abstract void work();
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- }
- package cn.it.hotel;
- /*
- * 定义厨师类
- * 属于员工一种,具有额外服务功能
- * 继承Employee,实现VIP接口
- */
- public class ChuShi extends Employee implements VIP{
- //空参数构造方法
- public ChuShi(){}
- //有参数构造方法
- public ChuShi(String name,String id){
- super(name,id);
- }
- //抽象方法重写
- public void work(){
- System.out.println("厨师在炒菜");
- }
- public void services(){
- System.out.println("厨师做菜加量");
- }
- }
- package cn.it.hotel;
- /*
- * 定义厨师类
- * 属于员工一种,具有额外服务功能
- * 继承Employee,实现VIP接口
- */
- public class FuWuYuan extends Employee implements VIP{
- //空参数构造方法
- public FuWuYuan() {
- super();
- }
- //满参数构造方法
- public FuWuYuan(String name, String id) {
- super(name, id);
- }
- //抽象方法重写
- public void work(){
- System.out.println("服务员在上菜");
- }
- public void services(){
- System.out.println("服务员给顾客倒酒");
- }
- }
- package cn.it.hotel;
- /*
- * 定义经理类
- * 属于员工一种,没有VIP功能
- * 自己有奖金属性
- */
- public class JingLi extends Employee {
- //空参数构造方法
- public JingLi(){}
- //满参数构造方法
- public JingLi(String name,String id,double money){
- super(name, id);
- this.money = money;
- }
- //定义奖金属性
- private double money;
- public double getMoney() {
- return money;
- }
- public void setMoney(double money) {
- this.money = money;
- }
- //重写抽象方法
- public void work(){
- System.out.println("管理,谁出错我罚谁");
- }
- }
- package cn.it.hotel;
- public class Test {
- public static void main(String[] args) {
- //创建1个经理,2个服务员,2个厨师
- JingLi jl = new JingLi("小名", "董事会001", 123456789.32);
- jl.work();
- FuWuYuan f1 = new FuWuYuan("翠花", "服务部001");
- FuWuYuan f2 = new FuWuYuan("酸菜", "服务部002");
- f1.work();
- f1.services();
- f2.work();
- f2.services();
- ChuShi c1 = new ChuShi("老干妈", "后厨001");
- ChuShi c2 = new ChuShi("老干爹", "后厨002");
- c1.work();
- c1.services();
- c2.work();
- c2.services();
- }
- }
- package cn.it.hotel;
- /*
- * 将的酒店员工,厨师,服务员,经理,分别存储到3个集合中
- * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法
- */
- import java.util.ArrayList;
- import java.util.Iterator;
- public class GenericTest {
- public static void main(String[] args) {
- //创建3个集合对象
- ArrayList<ChuShi> cs = new ArrayList<ChuShi>();
- ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>();
- ArrayList<JingLi> jl = new ArrayList<JingLi>();
- //每个集合存储自己的元素
- cs.add(new ChuShi("张三", "后厨001"));
- cs.add(new ChuShi("李四", "后厨002"));
- fwy.add(new FuWuYuan("翠花", "服务部001"));
- fwy.add(new FuWuYuan("酸菜", "服务部002"));
- jl.add(new JingLi("小名", "董事会001", 123456789.32));
- jl.add(new JingLi("小强", "董事会002", 123456789.33));
- // ArrayList<String> arrayString = new ArrayList<String>();
- iterator(jl);
- iterator(fwy);
- iterator(cs);
- }
- /*
- * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法 work
- * ? 通配符,迭代器it.next()方法取出来的是Object类型,怎么调用work方法
- * 强制转换: it.next()=Object o ==> Employee
- * 方法参数: 控制,可以传递Employee对象,也可以传递Employee的子类的对象
- * 泛型的限定 本案例,父类固定Employee,但是子类可以无限?
- * ? extends Employee 限制的是父类, 上限限定, 可以传递Employee,传递他的子类对象
- * ? super Employee 限制的是子类, 下限限定, 可以传递Employee,传递他的父类对象
- */
- public static void iterator(ArrayList<? extends Employee> array){
- Iterator<? extends Employee> it = array.iterator();
- while(it.hasNext()){
- //获取出的next() 数据类型,是什么Employee
- Employee e = it.next();
- e.work();
- }
- }
- }
- package cn.it.demo;
- import java.util.LinkedList;
- /*
- * LinkedList 链表集合的特有功能
- * 自身特点: 链表底层实现,查询慢,增删快
- *
- * 子类的特有功能,不能多态调用
- */
- public class LinkedListDemo {
- public static void main(String[] args) {
- function_3();
- }
- /*
- * E removeFirst() 移除并返回链表的开头
- * E removeLast() 移除并返回链表的结尾
- */
- public static void function_3(){
- LinkedList<String> link = new LinkedList<String>();
- link.add("1");
- link.add("2");
- link.add("3");
- link.add("4");
- String first = link.removeFirst();
- String last = link.removeLast();
- System.out.println(first);
- System.out.println(last);
- System.out.println(link);
- }
- /*
- * E getFirst() 获取链表的开头
- * E getLast() 获取链表的结尾
- */
- public static void function_2(){
- LinkedList<String> link = new LinkedList<String>();
- link.add("1");
- link.add("2");
- link.add("3");
- link.add("4");
- if(!link.isEmpty()){
- String first = link.getFirst();
- String last = link.getLast();
- System.out.println(first);
- System.out.println(last);
- }
- }
- public static void function_1(){
- LinkedList<String> link = new LinkedList<String>();
- link.addLast("a");
- link.addLast("b");
- link.addLast("c");
- link.addLast("d");
- link.addFirst("1");
- link.addFirst("2");
- link.addFirst("3");
- System.out.println(link);
- }
- /*
- * addFirst(E) 添加到链表的开头
- * addLast(E) 添加到链表的结尾
- */
- public static void function(){
- LinkedList<String> link = new LinkedList<String>();
- link.addLast("heima");
- link.add("abc");
- link.add("bcd");
- link.addFirst("itcast");
- System.out.println(link);
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- import java.util.List;
- /*
- * List接口派系, 继承Collection接口
- * 下面有很多实现类
- * List接口特点: 有序,索引,可以重复元素
- * 实现类, ArrayList, LinkedList
- *
- * List接口中的抽象方法,有一部分方法和他的父接口Collection是一样
- * List接口的自己特有的方法, 带有索引的功能
- */
- public class ListDemo {
- public static void main(String[] args) {
- function_2();
- }
- /*
- * E set(int index, E)
- * 修改指定索引上的元素
- * 返回被修改之前的元素
- */
- public static void function_2(){
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- Integer i = list.set(0, 5);
- System.out.println(i);
- System.out.println(list);
- }
- /*
- * E remove(int index)
- * 移除指定索引上的元素
- * 返回被删除之前的元素
- */
- public static void function_1(){
- List<Double> list = new ArrayList<Double>();
- list.add(1.1);
- list.add(1.2);
- list.add(1.3);
- list.add(1.4);
- Double d = list.remove(0);
- System.out.println(d);
- System.out.println(list);
- }
- /*
- * add(int index, E)
- * 将元素插入到列表的指定索引上
- * 带有索引的操作,防止越界问题
- * java.lang.IndexOutOfBoundsException
- * ArrayIndexOutOfBoundsException
- * StringIndexOutOfBoundsException
- */
- public static void function(){
- List<String> list = new ArrayList<String>();
- list.add("abc1");
- list.add("abc2");
- list.add("abc3");
- list.add("abc4");
- System.out.println(list);
- list.add(1, "itcast");
- System.out.println(list);
- }
- }
- package cn.it.demo;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /*
- * 迭代器的并发修改异常 java.util.ConcurrentModificationException
- * 就是在遍历的过程中,使用了集合方法修改了集合的长度,不允许的
- */
- public class ListDemo1 {
- public static void main(String[] args) {
- List<String> list = new ArrayList<String>();
- list.add("abc1");
- list.add("abc2");
- list.add("abc3");
- list.add("abc4");
- //对集合使用迭代器进行获取,获取时候判断集合中是否存在 "abc3"对象
- //如果有,添加一个元素 "ABC3"
- Iterator<String> it = list.iterator();
- while(it.hasNext()){
- String s = it.next();
- //对获取出的元素s,进行判断,是不是有"abc3"
- if(s.equals("abc3")){
- list.add("ABC3");
- }
- System.out.println(s);
- }
- }
- }
- package com.it.pojo;
- public class Student {
- private String name;
- private int age;
- public Student() {
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- //重写hashCode(), equals()方法
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Student student = (Student) o;
- if (age != student.age) return false;
- return name != null ? name.equals(student.name) : student.name == null;
- }
- @Override
- public int hashCode() {
- int result = name != null ? name.hashCode() : 0;
- result = 31 * result + age;
- return result;
- }
- /* @Override
- public int hashCode() {
- int result = name != null ? name.hashCode() : 0;
- result = 31 * result + age; //12: 0, 12, 1, 11 2,10 3, 9
- return result;
- //return 1; //我们可以实现, 对象的内容不同, 但是哈希值相同这种情况, 你知道可以这样写就行了, 实际开发几乎不用.
- }*/
- }
- package com.it.demo01_list;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- //案例: 演示List集合存储自定义对象并遍历.
- /*
- 使用集合的步骤:
- 1. 创建集合对象.
- 2. 创建元素对象.
- 3. 把元素对象添加到集合中.
- 4. 遍历集合.
- A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
- B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
- C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
- 一些快捷键:
- 迭代器遍历的快捷键: itit iterator iterator
- 普通for循环遍历数组: itar iterator array
- 普通for循环遍历集合: itli iterator list
- 增强for: iter iterator
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<Student> list = new ArrayList<>();
- //2. 创建元素对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("赵丽颖", 35);
- Student s3 = new Student("高圆圆", 31);
- //3. 把元素对象添加到集合中.
- list.add(s1);
- list.add(s2);
- list.add(s3);
- //4. 遍历集合.
- //方式一: 普通迭代器
- //A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
- Iterator<Student> it = list.iterator();
- //B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
- while (it.hasNext()) {
- //C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
- Student student = it.next();
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式二: 普通for循环.
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- System.out.println(student);
- }
- }
- }
- package com.it.demo01_list;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.ListIterator;
- /*
- 案例: 演示列表迭代器入门.
- 需求:
- 1.创建List集合, 用来存储字符串.
- 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
- 3.通过列表迭代器对List集合分别进行正向遍历和逆向遍历.
- List接口中的成员方法:
- public ListIterator<E> listIteator(); 根据List集合, 获取其对应的列表迭代器.
- ListIterator中的成员方法:
- public boolean hasNext(); 从Iterator迭代器中继承过来的, 判断是否有下一个元素.
- public E next(); 从Iterator迭代器中继承过来的, 获取下一个元素.
- public boolean hasPrevious(); ListIterator迭代器独有的成员方法, 判断是否有上一个元素.
- public E previous(); ListIterator迭代器独有的成员方法, 获取上一个元素.
- 细节(记忆):
- 1. 列表迭代器(ListIterator) 它是List体系独有的迭代器.
- 2. 使用列表迭代器进行逆向遍历之前, 必须通过该迭代器先进行一次正向遍历.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //需求: 演示列表迭代器.
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4. 遍历集合.
- //通过ListIterator列表迭代器实现: 集合元素的 正向遍历, 即: 从前往后.
- //获取列表迭代器对象.
- ListIterator<String> lit = list.listIterator();
- //判断是否有下一个元素
- while (lit.hasNext()) {
- //如果有, 就获取下一个元素
- String s = lit.next();
- //打印结果
- System.out.println(s);
- }
- System.out.println("-------------------------------");
- //通过ListIterator列表迭代器实现: 集合元素的 逆向遍历, 即: 从后往前.
- //判断是否有上一个元素
- while (lit.hasPrevious()) {
- //如果有, 就获取上一个元素
- String s = lit.previous();
- //打印结果
- System.out.println(s);
- }
- }
- }
- package com.it.demo01_list;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
- /*
- 案例: 演示并发修改异常.
- 需求:
- 1.创建List集合, 用来存储字符串.
- 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
- 3.判断集合中是否有"world"元素, 如果有, 就往集合中添加一个"JavaEE".
- 并发修改异常简介:
- 问题描述:
- 并发修改异常也叫ConcurrentModificationException, 指的是我们在使用 普通迭代器遍历集合的同时, 又往集合中添加元素了, 就会出现此问题.
- //大白话翻译: 我找你帮我打扫3层楼的卫生, 我给你1000块钱, 当你打扫到第2层的时候, 我说你只有打扫完整个4层楼的卫生, 我才给你1000, 你肯定不干.
- 产生原因:
- 当我们通过集合对象获取迭代器对象的时候, 迭代器内部会有一个变量记录此时集合中的元素个数, 如果实际要遍历的元素比这个值大, 就会报并发修改异常.
- 解决方案:
- 1. 通过普通for遍历List集合, 然后添加元素即可. 这种方式添加的元素, 是在整个集合元素的最后的.
- 2. 通过列表迭代器来遍历集合, 并且要通过列表迭代器的add()方法添加元素, 这种方式添加的元素, 只在指定元素的元素后添加的.
- //大白话翻译: 你是清洁工, 你比较好说话, 我们谈好的是你打扫3层, 我给你1000, 但是你看我4楼也比较脏, 你说义务帮我打扫.
- 3. 采用CopyOnWriteArrayList集合实现, 该集合能自动处理并发修改异常.
- */
- public class Demo03 {
- public static void main(String[] args) {
- //需求: 演示列表迭代器.
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4. 遍历集合.
- //普通的列表迭代器, 会产生并发修改异常.
- /* Iterator<String> it = list.iterator();
- while (it.hasNext()) {
- String s = it.next();
- if ("world".equals(s)){
- //如果当前元素是"world", 就往集合中添加一个"JavaEE".
- list.add("JavaEE");
- }
- }*/
- //解决方案一: 通过普通for遍历List集合,
- /*for (int i = 0; i < list.size(); i++) {
- String s = list.get(i);
- if ("world".equals(s)) {
- //如果当前元素是"world", 就往集合中添加一个"JavaEE".
- list.add("JavaEE");
- }
- }*/
- //解决方案二: 通过列表迭代器来遍历集合,
- /*ListIterator<String> lit = list.listIterator();
- while (lit.hasNext()) {
- String s = lit.next();
- if ("world".equals(s)) {
- //如果当前元素是"world", 就往集合中添加一个"JavaEE".
- lit.add("JavaEE"); //细节: 调用迭代器的添加元素的方法
- }
- }*/
- //5. 打印集合
- System.out.println("list: " + list);
- }
- }
- package com.it.demo01_list;
- import java.util.Iterator;
- import java.util.concurrent.CopyOnWriteArrayList;
- //案例: 演示CopyOnWriteArrayList集合, 解决并发修改异常.
- public class Demo04 {
- public static void main(String[] args) {
- //需求: 演示列表迭代器.
- //1. 创建集合对象.
- CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4. 遍历集合.
- Iterator<String> it = list.iterator();
- while (it.hasNext()) {
- String s = it.next();
- if ("world".equals(s)){
- //如果当前元素是"world", 就往集合中添加一个"JavaEE".
- list.add("JavaEE");
- }
- }
- //5. 打印集合
- System.out.println(list);
- }
- }
- package com.it.demo02_for;
- /*
- 案例: 演示增强for遍历数组.
- 需求:
- 1.定义int类型的数组, 存储元素1, 2, 3, 4, 5.
- 2.通过增强for, 遍历上述的数组.
- 增强for简介:
- 概述:
- 它是JDK1.5的特性, 是帮助我们简化遍历数组或者集合动作的.
- 格式:
- for(元素的数据类型 变量名 : 要遍历的集合或者数组) {
- //正常的逻辑代码
- }
- 快捷键:
- iter
- 本质:
- 增强for的底层就是迭代器实现的, 换言之: 增强for封装的就是迭代器.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 定义int数组
- int[] arr = {1, 2, 3, 4, 5};
- //2. 通过增强for遍历int数组.
- for (int num : arr) {
- System.out.println(num);
- }
- }
- }
- package com.it.demo02_for;
- import java.util.ArrayList;
- import java.util.List;
- //案例: 演示增强for出现并发修改异常, 说明: 增强for的底层就是一个普通的迭代器.
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- List<String> list = new ArrayList<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- list.add("hello");
- list.add("world");
- list.add("java");
- //4.通过增强for遍历上述的集合对象.
- for (String s : list) {
- //5.判断List集合中是否有"world", 如果有, 就往集合中添加元素"JavaEE".
- if ("world".equals(s)) {
- //如果当前元素是"world", 就往集合中添加一个"JavaEE".
- list.add("JavaEE");
- }
- }
- //6. 打印集合
- System.out.println("list: " + list);
- }
- }
- package com.it.demo03_exercise;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
- /*
- 案例: List集合存储学生对象, 并遍历. 4种方式实现.
- 遍历方式:
- 方式一: 增强for
- 方式二: 列表迭代器.
- 方式三: 普通for循环.
- 方式四: 普通迭代器.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- //多态
- List<Student> list = new ArrayList<>();
- //2. 创建元素对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("赵丽颖", 35);
- Student s3 = new Student("高圆圆", 31);
- //3. 把元素对象添加到集合中.
- list.add(s1);
- list.add(s2);
- list.add(s3);
- //4. 遍历集合.
- //方式一: 增强for
- for (Student student : list) {
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式二: 列表迭代器.
- ListIterator<Student> lit = list.listIterator();
- while (lit.hasNext()) {
- Student student = lit.next();
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式三: 普通for循环.
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式四: 普通迭代器.
- Iterator<Student> it = list.iterator();
- while (it.hasNext()) {
- Student student = it.next();
- System.out.println(student);
- }
- }
- }
- package com.it.demo03_exercise;
- import com.it.pojo.Student;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
- /*
- 案例: ArrayList集合存储学生对象, 并遍历. 4种方式实现.
- 遍历方式:
- 方式一: 增强for
- 方式二: 列表迭代器.
- 方式三: 普通for循环.
- 方式四: 普通迭代器.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- ArrayList<Student> list = new ArrayList<>();
- //2. 创建元素对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("赵丽颖", 35);
- Student s3 = new Student("高圆圆", 31);
- //3. 把元素对象添加到集合中.
- list.add(s1);
- list.add(s2);
- list.add(s3);
- //4. 遍历集合.
- //方式一: 增强for
- for (Student student : list) {
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式二: 列表迭代器.
- ListIterator<Student> lit = list.listIterator();
- while (lit.hasNext()) {
- Student student = lit.next();
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式三: 普通for循环.
- for (int i = 0; i < list.size(); i++) {
- Student student = list.get(i);
- System.out.println(student);
- }
- System.out.println("----------------------");
- //方式四: 普通迭代器.
- Iterator<Student> it = list.iterator();
- while (it.hasNext()) {
- Student student = it.next();
- System.out.println(student);
- }
- }
- }
- package com.it.demo04_linkedlist;
- import java.util.LinkedList;
- /*
- 案例: 演示LinkedList集合入门.
- 需求:
- 1.创建LinkedList集合对象, 存储字符串数据: "hello", "world", "java"
- 2.遍历LinkedList集合.
- 关于List体系的子类简介:
- 它的常用子类有ArrayList和LinkedList两个, 特点如下:
- ArrayList: 底层数据结构是数组, 所以查询快, 增删相对较慢.
- LinkedList: 底层数据结构是链表, 所以增删快, 查询相对较慢.
- 它们的相同点是: 元素有序, 可重复, 元素有索引, 因为都属于List体系.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建集合.
- LinkedList<String> list = new LinkedList<>();
- //2. 添加元素.
- list.add("hello");
- list.add("world");
- list.add("java");
- //3. 遍历, 有4种方式, 这里我只写一种, 其他的自己补.
- for (String s : list) {
- System.out.println(s);
- }
- }
- }
- package com.it.demo04_linkedlist;
- import java.util.LinkedList;
- /*
- 案例: 演示LinkedList集合的特有成员方法.
- 需求:
- 1.创建LinkedList集合对象, 存储字符串数据: "hello", "world", "java"
- 2.分别演示上述的6个方法.
- LinkedList简介:
- 它主要是操作集合的头和尾元素的, 所以里边也定义了大量的这类方法.
- 常用的成员方法如下:
- public void addFirst(E e) 往列表的开头插入指定的元素
- public void addLast(E e) 往列表的末尾插入指定的元素
- public E removeFirst() 删除列表中的第一个元素, 并返回被删除的元素
- public E removeLast() 删除列表中的最后一个元素, 并返回被删除的元素.
- public E getFirst() 返回列表的第一个元素
- public E getLast() 返回列表的最后一个元素
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建集合.
- LinkedList<String> list = new LinkedList<>();
- //2. 添加元素.
- list.add("hello");
- list.add("world");
- list.add("java");
- //3. 测试方法
- //测试添加
- //list.addFirst("hadoop");
- //list.addLast("linux"); //等价于: list.add("linux");
- //测试删除
- //System.out.println(list.removeFirst()); //hello
- //System.out.println(list.removeLast()); //java 等价于: list.remove("java");
- //测试获取
- System.out.println(list.getFirst()); //hello
- System.out.println(list.getLast()); //java
- //4. 打印集合.
- System.out.println("list: " + list);
- }
- }
- package com.it.demo05_set;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- /*
- 案例: 演示Set集合入门.
- 需求:
- 1.创建Set集合对象, 存储字符串数据: "hello", "world", "java", "world"
- 2.通过两种方式, 遍历Set集合.
- Set集合简介:
- 概述:
- 它属于Collection集合(单列集合)的子体系, 它是一个接口, 所以不能直接new,
- 它的元素特点是: 无序(存取顺序不一致), 唯一.
- 它的常用子类主要有:
- HashSet: 底层数据结构采用的是哈希表(就是数组 + 链表)实现的, 增删,查询相对都快.
- TreeSet: //目前了解.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- Set<String> hs = new HashSet<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hs.add("hello");
- hs.add("world");
- hs.add("java");
- hs.add("world");
- //4. 遍历集合.
- //方式一: 普通迭代器.
- Iterator<String> it = hs.iterator();
- while (it.hasNext()) {
- String s = it.next();
- System.out.println(s);
- }
- System.out.println("----------------------");
- //方式二: 增强for
- for (String h : hs) {
- System.out.println(h);
- }
- }
- }
- package com.it.demo05_set;
- import com.it.pojo.Student;
- /*
- 案例: 演示哈希值入门.
- 需求:
- 1.定义学生类, 属性为姓名和年龄.
- 2.在测试类的main方法中, 创建两个学生对象, 分别获取它们的哈希值, 并打印.
- 3.测试: 重写Object#hashCode()方法, 实现不同对象的哈希值也是相同的.
- 4.测试: 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
- 涉及到的Object类中的成员方法:
- public int hashCode();
- 获取指定对象的哈希值, 所谓的哈希值就是根据对象的属性等算出来的一个int类型的值.
- 因为在实际开发中, 我们认为如果对象的各个属性值都相同, 那么它们就是同一个对象,
- 所以子类一般都会重写Object#hashCode()方法, 采用根据属性值等来计算哈希值.
- 结论(记忆):
- 1. 同一对象不管调用多少次 hashCode()方法, 获取到的哈希值都是相同的.
- 2. 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //1. 创建两个学生对象.
- Student s1 = new Student("刘亦菲", 33);
- Student s2 = new Student("高圆圆", 35);
- //2. 打印它们的哈希值.
- //测试: 同一对象不管调用多少次 hashCode()方法, 获取到的哈希值都是相同的.
- System.out.println(s1.hashCode());
- System.out.println(s1.hashCode());
- System.out.println(s2.hashCode());
- System.out.println("------------------");
- //3. 测试: 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
- //重地和通话, 儿女和农丰
- System.out.println("重地".hashCode());
- System.out.println("通话".hashCode());
- System.out.println("------------------");
- System.out.println("儿女".hashCode());
- System.out.println("农丰".hashCode());
- }
- }
- package com.it.demo05_set;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- /*
- 案例: 演示HashSet集合入门.
- 需求:
- 1.创建Set集合对象, 存储字符串数据: "hello", "world", "java", "world"
- 2.通过两种方式, 遍历Set集合.
- Set集合简介:
- 概述:
- 它属于Collection集合(单列集合)的子体系, 它是一个接口, 所以不能直接new,
- 它的元素特点是: 无序(存取顺序不一致), 唯一, 元素无索引.
- 它的常用子类主要有:
- HashSet: 底层数据结构采用的是哈希表(就是数组 + 链表)实现的, 增删,查询相对都快.
- TreeSet: //目前了解.
- */
- public class Demo03 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- HashSet<String> hs = new HashSet<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hs.add("hello");
- hs.add("world");
- hs.add("java");
- hs.add("world");
- hs.add(null);
- hs.add(null);
- //4. 遍历集合.
- //方式一: 普通迭代器.
- Iterator<String> it = hs.iterator();
- while (it.hasNext()) {
- String s = it.next();
- System.out.println(s);
- }
- System.out.println("----------------------");
- //方式二: 增强for
- for (String h : hs) {
- System.out.println(h);
- }
- }
- }
- package com.it.demo05_set;
- import java.util.HashSet;
- import java.util.LinkedHashSet;
- import java.util.Set;
- /*
- 案例: 演示LinkedHashSet集合的使用.
- LinkedHashSet简介:
- 它是HashSet集合的子类, 底层是采用 链表 + 哈希表的形式实现的, 元素特点是: 有序, 唯一.
- 需求:
- 1.创建LinkedHashSet集合对象, 存储字符串"hello", "world", "java", "world"
- 2.遍历集合, 并将结果打印到控制台上.
- */
- public class Demo04 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- LinkedHashSet<String> lhs = new LinkedHashSet<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- lhs.add("hello");
- lhs.add("world");
- lhs.add("java");
- lhs.add("world");
- //4. 遍历集合.
- for (String s : lhs) {
- System.out.println(s);
- }
- }
- }
- package cn.it.demo1;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- /*
- * Set接口,特点不重复元素,没索引
- *
- * Set接口的实现类,HashSet (哈希表)
- * 特点: 无序集合,存储和取出的顺序不同,没有索引,不存储重复元素
- * 代码的编写上,和ArrayList完全一致
- */
- public class HashSetDemo {
- public static void main(String[] args) {
- Set<String> set = new HashSet<String>();
- set.add("cn");
- set.add("heima");
- set.add("java");
- set.add("java");
- set.add("itcast");
- Iterator<String> it = set.iterator();
- while(it.hasNext()){
- System.out.println(it.next());
- }
- System.out.println("==============");
- for(String s : set){
- System.out.println(s);
- }
- }
- }
- package cn.it.demo1;
- import java.util.HashSet;
- import cn.it.demo3.Person;
- /*
- * HashSet集合的自身特点:
- * 底层数据结构,哈希表
- * 存储,取出都比较快
- * 线程不安全,运行速度快
- */
- public class HashSetDemo1 {
- public static void main(String[] args) {
- /*HashSet<String> set = new HashSet<String>();
- set.add(new String("abc"));
- set.add(new String("abc"));
- set.add(new String("bbc"));
- set.add(new String("bbc"));
- System.out.println(set);*/
- //将Person对象中的姓名,年龄,相同数据,看作同一个对象
- //判断对象是否重复,依赖对象自己的方法 hashCode,equals
- HashSet<Person> setPerson = new HashSet<Person>();
- setPerson.add(new Person("a",11));
- setPerson.add(new Person("b",10));
- setPerson.add(new Person("b",10));
- setPerson.add(new Person("c",25));
- setPerson.add(new Person("d",19));
- setPerson.add(new Person("e",17));
- System.out.println(setPerson);
- }
- }
- package cn.it.demo1;
- import java.util.LinkedHashSet;
- /*
- * LinkedHashSet 基于链表的哈希表实现
- * 继承自HashSet
- *
- * LinkedHashSet 自身特性,具有顺序,存储和取出的顺序相同的
- * 线程不安全的集合,运行速度块
- */
- public class LinkedHashSetDemo {
- public static void main(String[] args) {
- LinkedHashSet<Integer> link = new LinkedHashSet<Integer>();
- link.add(123);
- link.add(44);
- link.add(33);
- link.add(33);
- link.add(66);
- link.add(11);
- System.out.println(link);
- }
- }
- package cn.it.demo3;
- /*
- * 对象的哈希值,普通的十进制整数
- * 父类Object,方法 public int hashCode() 计算结果int整数
- */
- public class HashDemo {
- public static void main(String[] args) {
- Person p = new Person();
- int i = p.hashCode();
- System.out.println(i);
- String s1 = new String("abc");
- String s2 = new String("abc");
- System.out.println(s1.hashCode());
- System.out.println(s2.hashCode());
- /*System.out.println("重地".hashCode());
- System.out.println("通话".hashCode());*/
- }
- }
- package cn.it.demo3;
- /*
- * 两个对象 Person p1 p2
- * 问题: 如果两个对象的哈希值相同 p1.hashCode()==p2.hashCode()
- * 两个对象的equals一定返回true吗 p1.equals(p2) 一定是true吗
- * 正确答案:不一定
- *
- * 如果两个对象的equals方法返回true,p1.equals(p2)==true
- * 两个对象的哈希值一定相同吗
- * 正确答案: 一定
- */
- public class HashDemo1 {
- }
- package cn.it.demo3;
- public class Person {
- private String name;
- private int age;
- /*
- * 没有做重写父类,每次运行结果都是不同整数
- * 如果子类重写父类的方法,哈希值,自定义的
- * 存储到HashSet集合的依据
- */
- public int hashCode(){
- return name.hashCode()+age*55;
- }
- //方法equals重写父类,保证和父类相同
- //public boolean equals(Object obj){}
- public boolean equals(Object obj){
- if(this == obj)
- return true;
- if(obj == null)
- return false;
- if(obj instanceof Person){
- Person p = (Person)obj;
- return name.equals(p.name) && age==p.age;
- }
- return false;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public Person(){}
- public String toString(){
- return name+".."+age;
- }
- }
- package com.it.demo06_change_variable;
- import javax.sound.midi.Soundbank;
- /*
- 案例: 可变参数详解.
- 需求:
- 1.定义getSum()方法, 用来获取n个整数的和(n可能是任意的一个数字).
- 2.在main方法中, 调用getSum()方法.
- 可变参数简介:
- 概述:
- 它是JDK1.5的特性, 也叫: 参数的个数可变, 一般是用作方法的形参列表的.
- 格式:
- 数据类型... 变量名 //就是在数据类型的后边加上 三个点
- 细节:
- 1. 可变参数的本质就是一个数组.
- 2. 一个方法形参列表有且只能有一个可变参数, 且可变参数必须放在参数列表的最后.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //2.在main方法中, 调用getSum()方法.
- int[] arr = {1, 2, 3, 4, 5};
- System.out.println(getSum(arr)); //不报错, 说明可变参数的本质就是一个数组
- System.out.println("----------------------");
- System.out.println(getSum()); //可变参数意味着参数个数可变, 里边可以传入的参数个数至少0个, 至多无所谓
- System.out.println(getSum(1, 2, 3)); //可变参数意味着参数个数可变, 里边可以传入的参数个数至少0个, 至多无所谓
- System.out.println("----------------------");
- }
- //1.定义getSum()方法, 用来获取n个整数的和(n可能是任意的一个数字).
- //方式二: 可变参数版
- // 一个方法形参列表有且只能有一个可变参数, 且可变参数必须放在参数列表的最后.
- //public static int getSum(int... arr, int... arr) { //报错, 一个方法形参列表有且只能有一个可变参数
- //public static int getSum(int... arr, int a) { //报错, 可变参数必须放在参数列表的最后.
- public static int getSum(int... arr) { //可变参数的本质就是一个数组
- int sum = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- return sum;
- }
- //方式一: 普通版
- /* public static int getSum(int a, int b) {
- return a + b;
- }
- public static int getSum(int a, int b, int c) {
- return a + b + c;
- }
- public static int getSum(int a, int b, int c, int d) {
- return a + b + c + d;
- }*/
- }
- package com.it.demo07_map;
- import java.util.HashMap;
- import java.util.Map;
- /*
- 案例: 演示Map集合入门.
- 需求:
- 1.定义Map集合, 键是学号, 值是学生的名字. (键值都是字符串类型).
- 2.往Map集合中添加3对元素.
- 3.打印Map集合对象.
- Map集合简介:
- 概述:
- 它是双列集合的顶层接口, 存储的是 键值对对象元素(由键和值两部分组成), 其中键具有唯一性, 值可以重复.
- 双列集合的数据结构只针对于 键 有效.
- 它的常用子类有: HashMap 和 TreeMap.
- 成员方法:
- public V put(K key, V value); 添加元素.
- */
- public class Demo01 {
- public static void main(String[] args) {
- //需求: 定义Map集合, 键是学号, 值是学生的名字. (键值都是字符串类型).
- //1. 创建集合.
- Map<String ,String> hm = new HashMap<>();
- //2. 创建元素.
- //3. 把元素添加到集合中.
- hm.put("heima001", "刘亦菲");
- hm.put("heima002", "赵丽颖");
- hm.put("heima003", "高圆圆");
- //4. 遍历集合, 因为Map集合的遍历方式和Collection不太一样, 我们稍后详解.
- //这里我们先直接输出Map集合.
- System.out.println(hm);
- }
- }
- package com.it.demo07_map;
- import java.util.HashMap;
- /*
- 案例: 演示Map集合的基本方法.
- 涉及到的Map集合中的成员方法:
- V put(K key,V value) 添加元素, 键不存在, 表示第一次添加, 返回null, 键存在, 表示重复添加, 会用新值覆盖旧值, 并返回旧值.
- V remove(Object key) 根据键删除键值对元素, 便返回删除之前的值, 如果键不存在, 返回null
- void clear() 移除所有的键值对元素
- boolean containsKey(Object key) 判断集合是否包含指定的键
- boolean containsValue(Object value) 判断集合是否包含指定的值
- boolean isEmpty() 判断集合是否为空
- int size() 集合的长度,也就是集合中键值对的个数
- 需求:
- 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
- 2.分别测试上述的7个方法.
- */
- public class Demo02 {
- public static void main(String[] args) {
- //需求: 定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
- //1. 创建集合对象.
- HashMap<String, String> hm = new HashMap<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hm.put("乔峰", "阿朱");
- hm.put("虚竹", "梦姑");
- hm.put("段誉", "王语嫣");
- hm.put("慕容复", "王语嫣");
- //测试: put()方法
- //System.out.println(hm.put("乔峰", "阿朱")); //null
- //System.out.println(hm.put("乔峰", "阿紫")); //阿朱
- //测试: V remove(Object key) 根据键删除键值对元素
- //System.out.println(hm.remove("乔峰")); //阿朱
- //System.out.println(hm.remove("杨过")); //null
- //测试: void clear() 移除所有的键值对元素
- //hm.clear();
- //测试: boolean containsKey(Object key) 判断集合是否包含指定的键
- //System.out.println(hm.containsKey("杨过")); //false
- //System.out.println(hm.containsKey("段誉")); //true
- //测试: boolean containsValue(Object value) 判断集合是否包含指定的值, 自己写.
- //测试: boolean isEmpty() 判断集合是否为空, 当且仅当集合的长度为0时, 返回true, 其他返回false
- //System.out.println(hm.isEmpty());
- //测试: int size() 集合的长度,也就是集合中键值对的个数
- System.out.println(hm.size()); //3
- //4. 打印集合.
- System.out.println(hm);
- }
- }
- package com.it.demo07_map;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Set;
- /*
- 案例: 演示Map集合的获取功能.
- 涉及到的Map集合中的成员方法:
- V get(Object key) 根据键获取值
- Set keySet() 获取所有键的集合
- Collection values() 获取所有值的集合
- Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的集合
- 需求:
- 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
- 2.先通过代码测试上述的3个方法. 即: get(), keySet(), values()
- */
- public class Demo03 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- HashMap<String, String> hm = new HashMap<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hm.put("乔峰", "阿朱");
- hm.put("虚竹", "梦姑");
- hm.put("段誉", "王语嫣");
- hm.put("慕容复", "王语嫣");
- //测试: V get(Object key) 根据键获取值
- System.out.println(hm.get("乔峰"));
- System.out.println(hm.get("虚竹"));
- System.out.println(hm.get("段誉"));
- System.out.println(hm.get("慕容复"));
- System.out.println("-----------------");
- //测试: Set keySet() 获取所有键的集合
- Set<String> keys = hm.keySet();
- //通过增强for遍历 Set集合
- for (String key : keys) {
- System.out.println(key);
- }
- System.out.println("-----------------");
- //测试: Collection values() 获取所有值的集合
- Collection<String> values = hm.values();
- for (String value : values) {
- System.out.println(value);
- }
- }
- }
- package com.it.demo07_map;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- /*
- 案例: 演示Map集合的遍历方式一, 根据键获取其对应的值.
- 需求:
- 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
- 2.往集合中添加3对键值对元素.
- 3.遍历Map集合.
- 思路:
- 大白话版: 根据丈夫找妻子.
- 1. 获取所有的丈夫的集合.
- 2. 遍历, 获取到每一个丈夫.
- 3. 根据丈夫找到其对应的妻子.
- 专业版:
- 1. 获取所有键的集合. Map#keySet()
- 2. 遍历, 获取到每一个键. 迭代器, 增强for.
- 3. 根据键获取其对应的值. Map#get();
- */
- public class Demo04 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- HashMap<String, String> hm = new HashMap<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hm.put("乔峰", "阿朱");
- hm.put("虚竹", "梦姑");
- hm.put("段誉", "王语嫣");
- //4. 遍历Map集合.
- //4.1 获取所有键的集合. Map#keySet()
- Set<String> keys = hm.keySet();
- //4.2 遍历, 获取到每一个键. 迭代器, 增强for.
- //方式一: 增强for
- for (String key : keys) {
- //4.3 根据键获取其对应的值. Map#get();
- String value = hm.get(key);
- System.out.println(key + "..." + value);
- }
- System.out.println("--------------------------");
- //方式二: 迭代器, 目前了解即可.
- Iterator<String> it = keys.iterator();
- while (it.hasNext()) {
- String key = it.next();
- String value = hm.get(key);
- System.out.println(key + "..." + value);
- }
- }
- }
- package com.it.demo07_map;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /*
- 案例: 演示Map集合的遍历方式二, 根据 键值对对象 获取其对应的 键和值.
- 需求:
- 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
- 2.往集合中添加3对键值对元素.
- 3.遍历Map集合.
- 思路:
- 大白话版: 根据 结婚证 找 丈夫和妻子.
- 1. 获取所有的 结婚证 的集合.
- 2. 遍历, 获取到每一个 结婚证.
- 3. 根据 结婚证 找到其对应的 丈夫和妻子.
- 专业版:
- 1. 获取所有 键值对对象 的集合. Map#entrySet() 键值对对象的数据类型: Map.Entry<K, V>
- 2. 遍历, 获取到每一个 键值对对象. 迭代器, 增强for.
- 3. 根据 键值对对象 获取其对应的 键和值. Map.Entry接口中的方法: getKey(), getValue();
- */
- public class Demo05 {
- public static void main(String[] args) {
- //1. 创建集合对象.
- HashMap<String, String> hm = new HashMap<>();
- //2. 创建元素对象.
- //3. 把元素对象添加到集合中.
- hm.put("乔峰", "阿朱");
- hm.put("虚竹", "梦姑");
- hm.put("段誉", "王语嫣");
- //4. 遍历Map集合.
- //4.1 获取所有 键值对对象 的集合.
- //Set<String> keys = hm.keySet(); //String: 键的数据类型
- Set<Map.Entry<String, String>> entrys = hm.entrySet(); //Map.Entry<String, String>: 键值对对象的数据类型
- //4.2 遍历, 获取到每一个 键值对对象.
- //方式一: 增强for
- for (Map.Entry<String, String> entry : entrys) {
- //entry: 就表示Map集合中的每一组元素, 例如: "虚竹", "梦姑"
- //4.3 根据 键值对对象 获取其对应的 键和值.
- System.out.println(entry.getKey() + "..." + entry.getValue());
- }
- System.out.println("--------------------------");
- //方式二: 迭代器, 目前了解即可.
- Iterator<Map.Entry<String, String>> it = entrys.iterator();
- while (it.hasNext()) {
- //entry: 就表示Map集合中的每一组元素, 例如: "虚竹", "梦姑"
- Map.Entry<String, String> entry = it.next();
- System.out.println(entry.getKey() + "..." + entry.getValue());
- }
- }
- }
- package cn.it.demo1;
- import java.util.HashMap;
- import java.util.Map;
- /*
- * Map接口中的常用方法
- * 使用Map接口的实现类 HashMap
- */
- public class MapDemo {
- public static void main(String[] args) {
- function_2();
- }
- /*
- * 移除集合中的键值对,返回被移除之前的值
- * V remove(K)
- */
- public static void function_2(){
- Map<Integer,String> map = new HashMap<Integer, String>();
- map.put(1, "a");
- map.put(2, "b");
- map.put(3, "c");
- System.out.println(map);
- String value = map.remove(33);
- System.out.println(value);
- System.out.println(map);
- }
- /*
- * 通过键对象,获取值对象
- * V get(K)
- * 如果集合中没有这个键,返回null
- */
- public static void function_1(){
- //创建集合对象,作为键的对象整数,值的对象存储字符串
- Map<Integer,String> map = new HashMap<Integer, String>();
- map.put(1, "a");
- map.put(2, "b");
- map.put(3, "c");
- System.out.println(map);
- String value = map.get(4);
- System.out.println(value);
- }
- /*
- * 将键值对存储到集合中
- * V put(K,V) K 作为键的对象, V作为值的对象
- * 存储的是重复的键,将原有的值,覆盖
- * 返回值一般情况下返回null,
- * 存储重复键的时候,返回被覆盖之前的值
- */
- public static void function(){
- //创建集合对象,HashMap,存储对象,键是字符串,值是整数
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- map.put("b", 2);
- map.put("c", 3);
- System.out.println(map);
- }
- }
- package cn.it.demo1;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /*
- * Map集合的遍历
- * 利用键获取值
- * Map接口中定义方法keySet
- * 所有的键,存储到Set集合
- */
- public class MapDemo1 {
- public static void main(String[] args) {
- /*
- * 1. 调用map集合的方法keySet,所有的键存储到Set集合中
- * 2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
- * 3. 调用map集合方法get,通过键获取到值
- */
- Map<String,Integer> map = new HashMap<String,Integer>();
- map.put("a", 11);
- map.put("b", 12);
- map.put("c", 13);
- map.put("d", 14);
- //1. 调用map集合的方法keySet,所有的键存储到Set集合中
- Set<String> set = map.keySet();
- //2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
- Iterator<String> it = set.iterator();
- while(it.hasNext()){
- //it.next返回是Set集合元素,也就是Map中的键
- //3. 调用map集合方法get,通过键获取到值
- String key = it.next();
- Integer value = map.get(key);
- System.out.println(key+"...."+value);
- }
- System.out.println("=======================");
- for(String key : map.keySet()){
- Integer value = map.get(key);
- System.out.println(key+"...."+value);
- }
- }
- }
- package cn.it.demo1;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /*
- * Map集合获取方式
- * entrySet方法,键值对映射关系(结婚证)获取
- * 实现步骤:
- * 1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
- * Set<Entry <K,V> >
- * 2. 迭代Set集合
- * 3. 获取出的Set集合的元素,是映射关系对象
- * 4. 通过映射关系对象方法 getKet, getValue获取键值对
- *
- * 创建内部类对象 外部类.内部类 = new
- */
- public class MapDemo2 {
- public static void main(String[] args) {
- Map<Integer,String> map = new HashMap<Integer, String>();
- map.put(1, "abc");
- map.put(2, "bcd");
- map.put(3, "cde");
- //1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
- Set<Map.Entry <Integer,String> > set = map.entrySet();
- //2. 迭代Set集合
- Iterator<Map.Entry <Integer,String> > it = set.iterator();
- while(it.hasNext()){
- // 3. 获取出的Set集合的元素,是映射关系对象
- // it.next 获取的是什么对象,也是Map.Entry对象
- Map.Entry<Integer, String> entry = it.next();
- //4. 通过映射关系对象方法 getKet, getValue获取键值对
- Integer key = entry.getKey();
- String value = entry.getValue();
- System.out.println(key+"...."+value);
- }
- System.out.println("=========================");
- for(Map.Entry<Integer, String> entry : map.entrySet()){
- System.out.println(entry.getKey()+"..."+entry.getValue());
- }
- }
- }
- package cn.it.demo2;
- import java.util.HashMap;
- import java.util.Map;
- /*
- * 使用HashMap集合,存储自定义的对象
- * 自定义对象,作为键,出现,作为值出现
- */
- public class HashMapDemo {
- public static void main(String[] args) {
- function_1();
- }
- /*
- * HashMap 存储自定义对象Person,作为键出现
- * 键的对象,是Person类型,值是字符串
- * 保证键的唯一性,存储到键的对象,重写hashCode equals
- */
- public static void function_1(){
- HashMap<Person, String> map = new HashMap<Person, String>();
- map.put(new Person("a",20), "里约热内卢");
- map.put(new Person("b",18), "索马里");
- map.put(new Person("b",18), "索马里");
- map.put(new Person("c",19), "百慕大");
- for(Person key : map.keySet()){
- String value = map.get(key);
- System.out.println(key+"..."+value);
- }
- System.out.println("===================");
- for(Map.Entry<Person, String> entry : map.entrySet()){
- System.out.println(entry.getKey()+"..."+entry.getValue());
- }
- }
- /*
- * HashMap 存储自定义的对象Person,作为值出现
- * 键的对象,是字符串,可以保证唯一性
- */
- public static void function(){
- HashMap<String, Person> map = new HashMap<String, Person>();
- map.put("beijing", new Person("a",20));
- map.put("tianjin", new Person("b",18));
- map.put("shanghai", new Person("c",19));
- for(String key : map.keySet()){
- Person value = map.get(key);
- System.out.println(key+"..."+value);
- }
- System.out.println("=================");
- for(Map.Entry<String, Person> entry : map.entrySet()){
- String key = entry.getKey();
- Person value = entry.getValue();
- System.out.println(key+"..."+value);
- }
- }
- }
- package cn.it.demo2;
- import java.util.Hashtable;
- import java.util.Map;
- /*
- * Map接口实现类 Hashtable
- * 底层数据结果哈希表,特点和HashMap是一样的
- * Hashtable 线程安全集合,运行速度慢
- * HashMap 线程不安全的集合,运行速度快
- *
- * Hashtable命运和Vector是一样的,从JDK1.2开始,被更先进的HashMap取代
- *
- * HashMap 允许存储null值,null键
- * Hashtable 不允许存储null值,null键
- *
- * Hashtable他的孩子,子类 Properties 依然活跃在开发舞台
- */
- public class HashtableDemo {
- public static void main(String[] args) {
- Map<String,String> map = new Hashtable<String,String>();
- map.put(null, null);
- System.out.println(map);
- }
- }
- package cn.it.demo2;
- import java.util.LinkedHashMap;
- /*
- * LinkedHashMap继承HashMap
- * 保证迭代的顺序
- */
- public class LinkedHashMapDemo {
- public static void main(String[] args) {
- LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
- link.put("1", "a");
- link.put("13", "a");
- link.put("15", "a");
- link.put("17", "a");
- System.out.println(link);
- }
- }
- package cn.it.demo2;
- public class Person {
- private String name;
- private int age;
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Person other = (Person) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public Person() {
- super();
- }
- @Override
- public String toString() {
- return "Person " + name +"...."+ age ;
- }
- }
- package cn.it.demo3;
- /*
- * JDK1.5新特性,静态导入
- * 减少开发的代码量
- * 标准的写法,导入包的时候才能使用
- *
- * import static java.lang.System.out;最末尾,必须是一个静态成员
- */
- import static java.lang.System.out;
- import static java.util.Arrays.sort;
- public class StaticImportDemo {
- public static void main(String[] args) {
- out.println("hello");
- int[] arr = {1,4,2};
- sort(arr);
- }
- }
- package cn.it.demo3;
- /*
- * JDK1.5新的特性,方法的可变参数
- * 前提: 方法参数数据类型确定,参数的个数任意
- * 可变参数语法: 数据类型...变量名
- * 可变参数,本质就是一个数组
- */
- public class VarArgumentsDemo {
- public static void main(String[] args) {
- //调用一个带有可变参数的方法,传递参数,可以任意
- // getSum();
- int sum = getSum(5,34,3,56,7,8,0);
- System.out.println(sum);
- function(1,2,3);
- }
- /*
- * 可变参数的注意事项
- * 1. 一个方法中,可变参数只能有一个
- * 2. 可变参数,必须写在参数列表的最后一位
- */
- public static void function(Object...o){
- }
- /*
- * 定义方法,计算10个整数和
- * 方法的可变参数实现
- */
- public static int getSum(int...a){
- int sum = 0 ;
- for(int i : a){
- sum = sum + i;
- }
- return sum;
- }
- /*
- * 定义方法,计算3个整数和
- */
- /*public static int getSum(int a,int b ,int c){
- return a+b+c;
- }*/
- /*
- * 定义方法,计算2个整数和
- */
- /*public static int getSum(int a,int b){
- return a+b;
- }*/
- }
- package cn.it.demo4;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /*
- * 集合操作的工具类
- * Collections
- */
- public class CollectionsDemo {
- public static void main(String[] args) {
- function_2();
- }
- /*
- * Collections.shuffle方法
- * 对List集合中的元素,进行随机排列
- */
- public static void function_2(){
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(5);
- list.add(9);
- list.add(11);
- list.add(8);
- list.add(10);
- list.add(15);
- list.add(20);
- System.out.println(list);
- //调用工具类方法shuffle对集合随机排列
- Collections.shuffle(list);
- System.out.println(list);
- }
- /*
- * Collections.binarySearch静态方法
- * 对List集合进行二分搜索,方法参数,传递List集合,传递被查找的元素
- */
- public static void function_1(){
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(5);
- list.add(8);
- list.add(10);
- list.add(15);
- list.add(20);
- //调用工具类静态方法binarySearch
- int index = Collections.binarySearch(list, 16);
- System.out.println(index);
- }
- /*
- * Collections.sort静态方法
- * 对于List集合,进行升序排列
- */
- public static void function(){
- //创建List集合
- List<String> list = new ArrayList<String>();
- list.add("ewrew");
- list.add("qwesd");
- list.add("Qwesd");
- list.add("bv");
- list.add("wer");
- System.out.println(list);
- //调用集合工具类的方法sort
- Collections.sort(list);
- System.out.println(list);
- }
- }
- package cn.it.demo5;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /*
- * Map集合的嵌套,Map中存储的还是Map集合
- * 要求:
- * 传智播客
- * Java基础班
- * 001 张三
- * 002 李四
- *
- * Java就业班
- * 001 王五
- * 002 赵六
- * 对以上数据进行对象的存储
- * 001 张三 键值对
- * Java基础班: 存储学号和姓名的键值对
- * Java就业班:
- * 传智播客: 存储的是班级
- *
- * 基础班Map <学号,姓名>
- * 传智播客Map <班级名字, 基础班Map>
- */
- public class MapMapDemo {
- public static void main(String[] args) {
- //定义基础班集合
- HashMap<String, String> javase = new HashMap<String, String>();
- //定义就业班集合
- HashMap<String, String> javaee = new HashMap<String, String>();
- //向班级集合中,存储学生信息
- javase.put("001", "张三");
- javase.put("002", "李四");
- javaee.put("001", "王五");
- javaee.put("002", "赵六");
- //定义传智播客集合容器,键是班级名字,值是两个班级容器
- HashMap<String, HashMap<String,String>> czbk =
- new HashMap<String, HashMap<String,String>>();
- czbk.put("基础班", javase);
- czbk.put("就业班", javaee);
- //keySet(czbk);
- entrySet(czbk);
- }
- public static void entrySet(HashMap<String,HashMap<String,String>> czbk){
- //调用czbk集合方法entrySet方法,将czbk集合的键值对关系对象,存储到Set集合
- Set<Map.Entry<String, HashMap<String,String>>>
- classNameSet = czbk.entrySet();
- //迭代器迭代Set集合
- Iterator<Map.Entry<String, HashMap<String,String>>> classNameIt = classNameSet.iterator();
- while(classNameIt.hasNext()){
- //classNameIt.next方法,取出的是czbk集合的键值对关系对象
- Map.Entry<String, HashMap<String,String>> classNameEntry = classNameIt.next();
- //classNameEntry方法 getKey,getValue
- String classNameKey = classNameEntry.getKey();
- //获取值,值是一个Map集合
- HashMap<String,String> classMap = classNameEntry.getValue();
- //调用班级集合classMap方法entrySet,键值对关系对象存储Set集合
- Set<Map.Entry<String, String>> studentSet = classMap.entrySet();
- //迭代Set集合
- Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator();
- while(studentIt.hasNext()){
- //studentIt方法next获取出的是班级集合的键值对关系对象
- Map.Entry<String, String> studentEntry = studentIt.next();
- //studentEntry方法 getKey getValue
- String numKey = studentEntry.getKey();
- String nameValue = studentEntry.getValue();
- System.out.println(classNameKey+".."+numKey+".."+nameValue);
- }
- }
- System.out.println("==================================");
- for (Map.Entry<String, HashMap<String, String>> me : czbk.entrySet()) {
- String classNameKey = me.getKey();
- HashMap<String, String> numNameMapValue = me.getValue();
- for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
- String numKey = nameMapEntry.getKey();
- String nameValue = nameMapEntry.getValue();
- System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
- }
- }
- }
- public static void keySet(HashMap<String,HashMap<String,String>> czbk){
- //调用czbk集合方法keySet将键存储到Set集合
- Set<String> classNameSet = czbk.keySet();
- //迭代Set集合
- Iterator<String> classNameIt = classNameSet.iterator();
- while(classNameIt.hasNext()){
- //classNameIt.next获取出来的是Set集合元素,czbk集合的键
- String classNameKey = classNameIt.next();
- //czbk集合的方法get获取值,值是一个HashMap集合
- HashMap<String,String> classMap = czbk.get(classNameKey);
- //调用classMap集合方法keySet,键存储到Set集合
- Set<String> studentNum = classMap.keySet();
- Iterator<String> studentIt = studentNum.iterator();
- while(studentIt.hasNext()){
- //studentIt.next获取出来的是classMap的键,学号
- String numKey = studentIt.next();
- //调用classMap集合中的get方法获取值
- String nameValue = classMap.get(numKey);
- System.out.println(classNameKey+".."+numKey+".."+nameValue);
- }
- }
- System.out.println("==================================");
- for(String className: czbk.keySet()){
- HashMap<String, String> hashMap = czbk.get(className);
- for(String numKey : hashMap.keySet()){
- String nameValue = hashMap.get(numKey);
- System.out.println(className+".."+numKey+".."+nameValue);
- }
- }
- }
- }
- package cn.it.demo6;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- /*
- * 实现模拟斗地主的功能
- * 1. 组合牌
- * 2. 洗牌
- * 3. 发牌
- * 4. 看牌
- */
- public class DouDiZhu {
- public static void main(String[] args) {
- //1. 组合牌
- //创建Map集合,键是编号,值是牌
- HashMap<Integer,String> pooker = new HashMap<Integer, String>();
- //创建List集合,存储编号
- ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
- //定义出13个点数的数组
- String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
- //定义4个花色数组
- String[] colors = {"","","",""};
- //定义整数变量,作为键出现
- int index = 2;
- //遍历数组,花色+点数的组合,存储到Map集合
- for(String number : numbers){
- for(String color : colors){
- pooker.put(index, color+number);
- pookerNumber.add(index);
- index++;
- }
- }
- //存储大王,和小王
- pooker.put(0, "大王");
- pookerNumber.add(0);
- pooker.put(1, "小王");
- pookerNumber.add(1);
- //洗牌,将牌的编号打乱
- Collections.shuffle(pookerNumber);
- //发牌功能,将牌编号,发给玩家集合,底牌集合
- ArrayList<Integer> player1 = new ArrayList<Integer>();
- ArrayList<Integer> player2 = new ArrayList<Integer>();
- ArrayList<Integer> player3 = new ArrayList<Integer>();
- ArrayList<Integer> bottom = new ArrayList<Integer>();
- //发牌采用的是集合索引%3
- for(int i = 0 ; i < pookerNumber.size() ; i++){
- //先将底牌做好
- if(i < 3){
- //存到底牌去
- bottom.add( pookerNumber.get(i));
- //对索引%3判断
- }else if(i % 3 == 0){
- //索引上的编号,发给玩家1
- player1.add( pookerNumber.get(i) );
- }else if( i % 3 == 1){
- //索引上的编号,发给玩家2
- player2.add( pookerNumber.get(i) );
- }else if( i % 3 == 2){
- //索引上的编号,发给玩家3
- player3.add( pookerNumber.get(i) );
- }
- }
- //对玩家手中的编号排序
- Collections.sort(player1);
- Collections.sort(player2);
- Collections.sort(player3);
- //看牌,将玩家手中的编号,到Map集合中查找,根据键找值
- //定义方法实现
- look("刘德华",player1,pooker);
- look("张曼玉",player2,pooker);
- look("林青霞",player3,pooker);
- look("底牌",bottom,pooker);
- }
- public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
- //遍历ArrayList集合,获取元素,作为键,到集合Map中找值
- System.out.print(name+" ");
- for(Integer key : player){
- String value = pooker.get(key);
- System.out.print(value+" ");
- }
- System.out.println();
- }
- }
Java 复习整理day07的更多相关文章
- java 复习整理(一 java简介和基础语法)
现在公司用的是封装太多东西的平台开发,觉着之前学的东西很多都忘了,所以想好好总结回顾一下.之前总是想学很多编程语言像python.s6.node.react,但现在越来越体会到编程语言只是一个开发的工 ...
- Java 复习整理day08
package com.it.demo02_lambda; //接口, 表示动物. //public abstract class Animal { //报错, Lambda表达式只针对于接口有效 p ...
- java复习整理(六 异常处理)
一.异常简介 在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出).Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性. ...
- java 复习整理(五 类加载机制与对象初始化)
类加载机制与对象初始化 一 . 类加载机制 类加载机制是指.class文件加载到jvm并形成Class对象的机制.之后应用可对Class对象进行实例化并调用.类加载机制可在运行时动态加载外部的类, ...
- java 复习整理(四 String类详解)
String 类详解 StringBuilder与StringBuffer的功能基本相同,不同之处在于StringBuilder是非线程安全的,而StringBuffer是线程安全的,因此效率上S ...
- java 复习整理(三 修饰符)
访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为default,在同一包内可见,不使用任何修饰符. 私有的,以pr ...
- java 复习整理(二 数据类型和几种变量)
源文件声明规则 当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则. 一个源文件中只能有一个public类 一个源文件可以有多个非public类 源文件的名 ...
- Java 复习整理day10
package com.it.demo01_quickstart; /* 案例: 讲解网络编程相关概念. 网络编程简介: 概述: 网络编程也叫: 套接字编程, Socket编程, 就是用来实现 网络互 ...
- Java 复习整理day09
package com.it.demo01_thread; /* 案例: 多线程简介. 概述: 指的是进程有多条执行路径, 统称叫: 多线程. 进程: 指的是可执行程序, 文件(例如: .exe) 大 ...
随机推荐
- 项目中处理数据常用Excel公式
="'"&A1&"'," 需求:是大佬给了excel,里面是700多个单号,要我从生产的数据库中查询出每个单号对应的类型,这时需要查数据库,我决 ...
- 打算写一些Netty的文章了,先聊聊为什么要学习Netty
微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 2021年了,终于开始系统性总结Netty相关的东西了. 这会是Netty系列的第一篇,我想先聊聊 "为 ...
- redis存json数据时选择string还是hash
redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...
- CTFHub - Web(六)
命令注入: 1.进入页面,测试127.0.0.1, 关键代码: <?php $res = FALSE; if (isset($_GET['ip']) && $_GET['ip'] ...
- 攻防世界 - Web(一)
baby_web: 1.根据题目提示,初始页面即为index,将1.php改为index.php,发现依然跳转成1.php,尝试修改抓包,出现如下回显, 2.在header中获取flag, flag: ...
- Ubuntu16.04安装MySQL8.0
1.Ubuntu换源(阿里云) sudo cp /etc/apt/sources.list /etc/apt/sources.list.baksudo vi /etc/apt/sources.list ...
- linux系统中set、env、export关系
set 用来显示shell变量(包括环境变量.用户变量和函数名及其定义),同时可以设置shell选项来开启调试.变量扩展.路径扩展等开关env 用来显示和设置环境变量export 用来显示和设置导出到 ...
- 二十七:XSS跨站之代码及httponly绕过
httponly:如果给某个 cookie 设置了 httpOnly 属性,则无法通过 JS 脚本 读取到该 cookie 的信息,但还Application 中手动修改 cookie,所以只是在一定 ...
- 1.5V升压3V集成电路升压芯片
干电池1.5V升压3V的升压芯片,适用于干电池升压产品输出3V供电 1.5V输入时,输出3V,电流可达500MA. PW5100是一款效率大.10uA低功耗 PW5100输入电压:0.7V-5V PW ...
- 前端PDF文件转图片方法
第一步:先下载pdfjs,网址:PDF下载地址,再引入到项目中,我是标签直接引用的 <script src="pdfjs/build/pdf.js"></scri ...