1. package com.it.demo05_innerclass;
  2.  
  3. /*
  4. 案例: 演示内部类入门.
  5.  
  6. 概述:
  7. 所谓的内部类指的是类里边还有一个类, 里边那个类叫: 内部类, 外边那个类, 叫外部类.
  8. 分类:
  9. 成员内部类:
  10. 定义在成员位置的内部类.
  11. 局部内部类:
  12. 定义在局部位置的内部类.
  13. */
  14. public class A { //外部类
  15. //成员变量
  16. public int age = 10;
  17.  
  18. //成员内部类
  19. public class B {
  20.  
  21. }
  22.  
  23. //成员方法
  24. public void show() {
  25. //局部变量
  26. int age = 10;
  27.  
  28. //局部内部类
  29. class B {
  30.  
  31. }
  32. }
  33. }
  1. package com.it.demo05_innerclass;
  2.  
  3. //自定义的抽象类, 表示动物类
  4. public abstract class Animal {
  5. //抽象方法, 表示: 吃
  6. public abstract void eat();
  7. }
  8.  
  9. package com.it.demo05_innerclass;
  10.  
  11. //子类, 猫类
  12. public class Cat extends Animal {
  13. @Override
  14. public void eat() {
  15. System.out.println("猫吃鱼!");
  16. }
  17. }
  18.  
  19. package com.it.demo05_innerclass;
  20.  
  21. /*
  22. 案例: 匿名内部类入门
  23.  
  24. 需求:
  25. 1.定义Animal抽象类, 该类中有一个抽象方法eat().
  26. 2.在测试类的main方法中, 通过匿名内部类的形式创建Animal抽象类的子类对象.
  27. 3.调用Animal类中的eat()方法
  28.  
  29. 匿名内部类简介:
  30. 概述:
  31. 匿名内部类指的是没有名字的局部内部类.
  32. 格式:
  33. new 类名或者接口名(){
  34. //重写类或者接口中所有的抽象方法
  35. };
  36. 前提条件:
  37. 必须有一个类或者接口.
  38. 本质:
  39. 专业版解释: 匿名内部类就是一个继承了类或者实现了接口的匿名的子类对象.
  40. 大白话翻译: 匿名内部类不是类, 而是一个 子类对象.
  41. 应用场景:
  42. 1. 当对 对象方法(也叫成员方法)仅调用一次的时候.
  43. 2. 可以作为方法的实参进行传递.
  44. */
  45. public class AnimalTest {
  46. public static void main(String[] args) {
  47. //需求: 调用Animal#eat()
  48. //方式一: 普通调用.
  49. //创建一个 继承了Animal类的子类的对象, 子类名叫: Cat, 对象名叫: an
  50. Animal an = new Cat();
  51. an.eat();
  52.  
  53. //方式二: 匿名对象实现.
  54. //创建一个 继承了Animal类的子类的对象, 子类名叫: Cat, 对象名叫: 不知道(匿名)
  55. new Cat().eat();
  56.  
  57. //方式三: 匿名内部类的方式实现.
  58. //创建一个 继承了Animal类的子类的对象, 子类名叫: 不知道, 对象名叫: 不知道(匿名)
  59. new Animal(){
  60. //重写类或者接口中所有的抽象方法
  61. @Override
  62. public void eat() {
  63. System.out.println("我是匿名内部类, 爱吃啥吃啥!");
  64. }
  65. }.eat();
  66. }
  67. }
  68.  
  69. package com.it.demo05_innerclass;
  70.  
  71. /*
  72. 案例: 演示匿名内部类的应用场景.
  73.  
  74. 应用场景:
  75. 1. 当对 对象方法(也叫成员方法)仅调用一次的时候.
  76. 2. 可以作为方法的实参进行传递.
  77.  
  78. 关于匿名内部类使用时的一个小技巧:
  79. 一般是抽象类或者接口中的抽象方法不超过3个的时候(绝大多数是1个), 就可以考虑使用匿名内部类了.
  80. */
  81. public class AnimalTest02 {
  82. public static void main(String[] args) {
  83. //演示使用场景1: 当对 对象方法(也叫成员方法)仅调用一次的时候.
  84. //如果多次调用, 复杂写法如下
  85. //method01();
  86.  
  87. //简化版: 通过多态实现.
  88. Animal an = new Animal(){ //父类引用指向子类对象.
  89. @Override
  90. public void eat() {
  91. System.out.println("匿名内部类, 随便吃点啥!");
  92. }
  93. };
  94. an.eat();
  95. an.eat();
  96. an.eat();
  97. System.out.println("--------------------------");
  98.  
  99. //演示匿名内部类的使用场景2: 可以作为方法的实参进行传递.
  100. //printAnimal(这里要的是Animal类的子类对象);
  101. //printAnimal(an); //简写版
  102.  
  103. //标准版
  104. //printAnimal(new Cat());
  105. printAnimal(new Animal(){ //匿名内部类的本质就是: 一个子类对象
  106. @Override
  107. public void eat() {
  108. System.out.println("匿名内部类, 可以作为方法的实参进行传递!");
  109. }
  110. });
  111. }
  112.  
  113. //定义方法, 接收Animal对象, 调用eat()方法
  114. public static void printAnimal(Animal an) {
  115. an.eat();
  116. }
  117.  
  118. //演示匿名内部类的重复调用.
  119. public static void method01() {
  120. new Animal(){
  121. @Override
  122. public void eat() {
  123. System.out.println("匿名内部类, 随便吃点啥!");
  124. }
  125. }.eat();
  126.  
  127. //重复调用
  128. new Animal(){
  129. @Override
  130. public void eat() {
  131. System.out.println("匿名内部类, 随便吃点啥!");
  132. }
  133. }.eat();
  134. }
  135. }
  1. package cn.it.demo01;
  2. /*
  3. * 实现正则规则和字符串进行匹配,使用到字符串类的方法
  4. * String类三个和正则表达式相关的方法
  5. * boolean matches(String 正则的规则)
  6. * "abc".matches("[a]") 匹配成功返回true
  7. *
  8. * String[] split(String 正则的规则)
  9. * "abc".split("a") 使用规则将字符串进行切割
  10. *
  11. * String replaceAll( String 正则规则,String 字符串)
  12. * "abc0123".repalceAll("[\\d]","#")
  13. * 安装正则的规则,替换字符串
  14. */
  15. public class RegexDemo {
  16. public static void main(String[] args) {
  17. checkTel();
  18. }
  19.  
  20. /*
  21. * 检查手机号码是否合法
  22. * 1开头 可以是34578 0-9 位数固定11位
  23. */
  24. public static void checkTel(){
  25. String telNumber = "1335128005";
  26. //String类的方法matches
  27. boolean b = telNumber.matches("1[34857][\\d]{9}");
  28. System.out.println(b);
  29. }
  30.  
  31. /*
  32. * 检查QQ号码是否合法
  33. * 0不能开头,全数字, 位数5,10位
  34. * 123456
  35. * \\d \\D匹配不是数字
  36. */
  37. public static void checkQQ(){
  38. String QQ = "123456";
  39. //检查QQ号码和规则是否匹配,String类的方法matches
  40. boolean b = QQ.matches("[1-9][\\d]{4,9}");
  41. System.out.println(b);
  42. }
  43. }
  1. package cn.it.demo01;
  2.  
  3. public class RegexDemo1 {
  4. public static void main(String[] args) {
  5. replaceAll_1();
  6. }
  7.  
  8. /*
  9. * "Hello12345World6789012"将所有数字替换掉
  10. * String类方法replaceAll(正则规则,替换后的新字符串)
  11. */
  12. public static void replaceAll_1(){
  13. String str = "Hello12345World6789012";
  14. str = str.replaceAll("[\\d]+", "#");
  15. System.out.println(str);
  16. }
  17. /*
  18. * String类方法split对字符串进行切割
  19. * 192.168.105.27 按照 点切割字符串
  20. */
  21. public static void split_3(){
  22. String ip = "192.168.105.27";
  23. String[] strArr = ip.split("\\.");
  24. System.out.println("数组的长度"+strArr.length);
  25. for(int i = 0 ; i < strArr.length ; i++){
  26. System.out.println(strArr[i]);
  27. }
  28. }
  29.  
  30. /*
  31. * String类方法split对字符串进行切割
  32. * 18 22 40 65 按照空格切割字符串
  33. */
  34. public static void split_2(){
  35. String str = "18 22 40 65";
  36. String[] strArr = str.split(" +");
  37. System.out.println("数组的长度"+strArr.length);
  38. for(int i = 0 ; i < strArr.length ; i++){
  39. System.out.println(strArr[i]);
  40. }
  41. }
  42.  
  43. /*
  44. * String类方法split对字符串进行切割
  45. * 12-25-36-98 按照-对字符串进行切割
  46. */
  47. public static void split_1(){
  48. String str = "12-25-36-98";
  49. //按照-对字符串进行切割,String类方法split
  50. String[] strArr = str.split("-");
  51. System.out.println("数组的长度"+strArr.length);
  52. for(int i = 0 ; i < strArr.length ; i++){
  53. System.out.println(strArr[i]);
  54. }
  55. }
  56. }
  1. package cn.it.demo01;
  2.  
  3. public class RegexDemo2 {
  4. public static void main(String[] args) {
  5. checkMail();
  6. }
  7. /*
  8. * 检查邮件地址是否合法
  9. * 规则:
  10. * 1234567@qq.com
  11. * mym_ail@sina.com
  12. * nimail@163.com
  13. * wodemail@yahoo.com.cn
  14. *
  15. * @: 前 数字字母_ 个数不能少于1个
  16. * @: 后 数字字母 个数不能少于1个
  17. * .: 后面 字母
  18. *
  19. */
  20. public static void checkMail(){
  21. String email ="abc123@sina.com";
  22. boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
  23. System.out.println(b);
  24. }
  25. }
  1. package com.it.demo06_collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6. /*
  7. 案例: Collection集合入门.
  8.  
  9. 需求:
  10. 1.创建Collection集合对象, 用来存储字符串.
  11. 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
  12. "hello", "world", "java"
  13. 3.通过输出语句, 直接打印集合对象, 并观察结果.
  14.  
  15. 涉及到的知识点:
  16. 泛型:
  17. 概述:
  18. 泛指某种特定的数据类型.
  19. 格式:
  20. <数据类型>
  21. 细节:
  22. 1. 实际开发中, 泛型一般是只和集合相结合使用的, 泛型是用来约束集合中存储元素的数据类型的.
  23. 例如:
  24. Collection<String> 说明集合中只能添加 字符串.
  25. Collection<Integer> 说明集合中只能添加 整数.
  26. 2. 泛型只能是引用类型.
  27. 3. 前后泛型必须保持一致, 或者后边的泛型可以省略不写(这个是JDK1.7的特性: 叫菱形泛型)
  28. 4. 泛型一般用字母E, T, K, V表示:
  29. E: Element(元素), T: Type(类型), K: Key(键), V: Value(值)
  30. Collection接口中的成员方法:
  31. public boolean add(T t); 往集合中添加元素.
  32. */
  33. public class Demo01 {
  34. public static void main(String[] args) {
  35. //1. 创建Collection集合对象.
  36. //Collection list = new Collection(); //报错: Collection是接口, 不能直接new.
  37. //多态方式实现
  38. Collection<String> list = new ArrayList<>();
  39. //2. 往集合中添加元素.
  40. //list.add(10); //报错, 原因是因为: 集合的泛型是String, 说明该集合中只能添加字符串.
  41. list.add("hello");
  42. list.add("world");
  43. list.add("java");
  44.  
  45. //3. 打印集合对象.
  46. System.out.println(list);
  47. }
  48. }
  1. package com.it.demo06_collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6. /*
  7. 案例: 演示Collection集合中的成员方法:
  8.  
  9. 涉及到的Collection接口中的成员方法:
  10. public boolean add(E e) 添加元素.
  11. public boolean remove(Object obj) 从集合中移除指定的元素.
  12. public void clear() 清空集合对象
  13. public boolean contains(Object obj) 判断集合中是否包含指定的元素
  14. public boolean isEmpty() 判断集合是否为空
  15. public int size() 获取集合的长度, 即集合中元素的个数
  16.  
  17. 需求:
  18. 1.通过多态的形式, 创建Collection集合对象.
  19. 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
  20. "hello", "world", "java"
  21. 3.分别测试上述的6个成员方法.
  22. */
  23. public class Demo02 {
  24. public static void main(String[] args) {
  25. //1. 创建集合对象. 多态形式创建.
  26. Collection<String> list = new ArrayList<>();
  27. //2. 往集合中添加三个元素.
  28. System.out.println(list.add("hello")); //返回值是true: 因为ArrayList的特点是: 可重复.
  29. //System.out.println(list.add("hello")); //返回值是true: 因为ArrayList的特点是: 可重复.
  30. list.add("world");
  31. list.add("java");
  32. System.out.println("-----------------------");
  33.  
  34. //3. 测试Collection集合中的成员方法
  35. //测试 public boolean remove(Object obj) 从集合中移除指定的元素, 存在就删除并返回true, 不存在就返回false
  36. /*System.out.println(list.remove("world")); //true
  37. System.out.println(list.remove("world123")); //false*/
  38. System.out.println("-----------------------");
  39.  
  40. //测试public void clear() 清空集合对象
  41. //list.clear();
  42. System.out.println("-----------------------");
  43.  
  44. //测试 public boolean contains(Object obj) 判断集合中是否包含指定的元素
  45. System.out.println(list.contains("world")); //true
  46. System.out.println(list.contains("world123")); //false
  47. System.out.println("-----------------------");
  48.  
  49. //测试 public boolean isEmpty(), 判断集合是否为空, 当且仅当集合中元素长度为0的时候, 返回true.
  50. //System.out.println(list.isEmpty()); //false, 说明集合有数据.
  51.  
  52. //测试 public int size() 获取集合的长度, 即集合中元素的个数
  53. System.out.println("集合的长度为: " + list.size());
  54.  
  55. //4. 打印集合对象
  56. System.out.println(list);
  57. }
  58. }
  1. package com.it.demo06_collection;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6.  
  7. /*
  8. 案例: 演示遍历Collection集合.
  9.  
  10. 需求:
  11. 1.通过多态的形式, 创建Collection集合对象.
  12. 2.调用Collection接口的add()方法, 往上述集合中添加3个字符串, 内容如下:
  13. "hello", "world", "java"
  14. 3.通过迭代器遍历集合, 获取到每一个元素, 并打印到控制台上.
  15.  
  16. 涉及到的成员方法:
  17. Collection接口中成员方法:
  18. public boolean add(E e); //添加元素到集合中.
  19. public Iterator<E> iterator(); //根据集合对象, 获取其对应的迭代器对象.
  20. 迭代器(Iterator)中的成员方法:
  21. public boolean hasNext(); //判断迭代器中是否有下一个元素.
  22. public E next(); //有就获取该元素.
  23.  
  24. 结论(记忆): 使用集合时的步骤(4大步, 3小步)
  25. 1. 创建集合对象.
  26. 2. 创建元素对象.
  27. 3. 把元素对象添加到集合中.
  28. 4. 遍历集合.
  29. A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
  30. B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
  31. C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
  32. */
  33. public class Demo03 {
  34. public static void main(String[] args) {
  35. //1. 创建Collection集合对象.
  36. Collection<String> list = new ArrayList<>(); //list集合对象相当于: 仓库
  37. //2. 把字符串对象添加到Collection集合中.
  38. list.add("hello");
  39. list.add("world");
  40. list.add("java");
  41. //3. 根据集合对象, 获取其对应的迭代器对象.
  42. Iterator<String> it = list.iterator(); //it: 迭代器对象, 相当于仓库管理员.
  43. //4. 判断迭代器中是否有下一个元素,
  44. while (it.hasNext()) {
  45. //s: 就是集合中的每一个元素
  46. //如果有就通过next()方法获取数据
  47. String s = it.next();
  48. //5. 将获取获取到的数据, 打印到控制台上.
  49. System.out.println(s);
  50. }
  51. }
  52. }
  1. package cn.it.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6. /*
  7. * Collection接口中的方法
  8. * 是集合中所有实现类必须拥有的方法
  9. * 使用Collection接口的实现类,程序的演示
  10. * ArrayList implements List
  11. * List extends Collection
  12. * 方法的执行,都是实现的重写
  13. */
  14. public class CollectionDemo {
  15. public static void main(String[] args) {
  16. function_3();
  17. }
  18. /*
  19. * Collection接口方法
  20. * boolean remove(Object o)移除集合中指定的元素
  21. */
  22. private static void function_3(){
  23. Collection<String> coll = new ArrayList<String>();
  24. coll.add("abc");
  25. coll.add("money");
  26. coll.add("itcast");
  27. coll.add("itheima");
  28. coll.add("money");
  29. coll.add("123");
  30. System.out.println(coll);
  31.  
  32. boolean b = coll.remove("money");
  33. System.out.println(b);
  34. System.out.println(coll);
  35. }
  36.  
  37. /* Collection接口方法
  38. * Object[] toArray() 集合中的元素,转成一个数组中的元素, 集合转成数组
  39. * 返回是一个存储对象的数组, 数组存储的数据类型是Object
  40. */
  41. private static void function_2() {
  42. Collection<String> coll = new ArrayList<String>();
  43. coll.add("abc");
  44. coll.add("itcast");
  45. coll.add("itheima");
  46. coll.add("money");
  47. coll.add("123");
  48.  
  49. Object[] objs = coll.toArray();
  50. for(int i = 0 ; i < objs.length ; i++){
  51. System.out.println(objs[i]);
  52. }
  53. }
  54. /*
  55. * 学习Java中三种长度表现形式
  56. * 数组.length 属性 返回值 int
  57. * 字符串.length() 方法,返回值int
  58. * 集合.size()方法, 返回值int
  59. */
  60.  
  61. /*
  62. * Collection接口方法
  63. * boolean contains(Object o) 判断对象是否存在于集合中,对象存在返回true
  64. * 方法参数是Object类型
  65. */
  66. private static void function_1() {
  67. Collection<String> coll = new ArrayList<String>();
  68. coll.add("abc");
  69. coll.add("itcast");
  70. coll.add("itheima");
  71. coll.add("money");
  72. coll.add("123");
  73.  
  74. boolean b = coll.contains("itcast");
  75. System.out.println(b);
  76. }
  77.  
  78. /*
  79. * Collection接口的方法
  80. * void clear() 清空集合中的所有元素
  81. * 集合容器本身依然存在
  82. */
  83. public static void function(){
  84. //接口多态的方式调用
  85. Collection<String> coll = new ArrayList<String>();
  86. coll.add("abc");
  87. coll.add("bcd");
  88. System.out.println(coll);
  89.  
  90. coll.clear();
  91.  
  92. System.out.println(coll);
  93.  
  94. }
  95. }
  1. package cn.it.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6.  
  7. public class CollectionDemo1 {
  8. public static void main(String[] args) {
  9. //集合可以存储任意类型的对象
  10. //集合中,不指定存储的数据类型, 集合什么都存
  11. Collection coll = new ArrayList();
  12. coll.add("abc");
  13. coll.add("uyjgtfd");
  14.  
  15. //迭代器获取
  16. Iterator it = coll.iterator();
  17. while(it.hasNext()){
  18. //it.next()获取出来的是什么数据类型,Object类
  19. //Object obj = it.next();
  20. //System.out.println(obj);
  21. String s = (String)it.next();
  22. System.out.println(s.length());
  23. }
  24. }
  25. }
  1. package cn.it.demo;
  2.  
  3. public class Person {
  4. private String name;
  5. private int age;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public int getAge() {
  13. return age;
  14. }
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18.  
  19. /*public String toString() {
  20. return "Person [name=" + name + ", age=" + age + "]";
  21. }*/
  22. public String toString(){
  23. return name+"..."+age;
  24. }
  25. public Person(){}
  26. public Person(String name, int age) {
  27. super();
  28. this.name = name;
  29. this.age = age;
  30. }
  31.  
  32. }
  33.  
  34. package cn.it.demo;
  35.  
  36. import java.util.ArrayList;
  37. /*
  38. * 集合体系,
  39. * 目标 集合本身是一个存储的容器:
  40. * 必须使用集合存储对象
  41. * 遍历集合,取出对象
  42. * 集合自己的特性
  43. */
  44. public class ArrayListDemo {
  45. public static void main(String[] args) {
  46. /*
  47. * 集合ArrayList,存储int类型数
  48. * 集合本身不接受基本类,自动装箱存储
  49. */
  50. ArrayList<Integer> array = new ArrayList<Integer>();
  51. array.add(11);
  52. array.add(12);
  53. array.add(13);
  54. array.add(14);
  55. array.add(15);
  56. for(int i = 0 ; i < array.size() ;i++){
  57. System.out.println(array.get(i));
  58. }
  59. /*
  60. * 集合存储自定义的Person类的对象
  61. */
  62. ArrayList<Person> arrayPer = new ArrayList<Person>();
  63. arrayPer.add(new Person("a",20));
  64. arrayPer.add(new Person("b",18));
  65. arrayPer.add(new Person("c",22));
  66. for(int i = 0 ; i < arrayPer.size();i++){
  67. //get(0),取出的对象Person对象
  68. //打印的是一个对象,必须调用的toString()
  69. System.out.println(arrayPer.get(i));
  70. }
  71. }
  72. }
  1. package cn.it.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6.  
  7. /*
  8. * 集合中的迭代器:
  9. * 获取集合中元素方式
  10. * 接口 Iterator : 两个抽象方法
  11. * boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true
  12. * next() 取出集合中的下一个元素
  13. *
  14. * Iterator接口,找实现类.
  15. * Collection接口定义方法
  16. * Iterator iterator()
  17. * ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
  18. * 使用ArrayList集合的对象
  19. * Iterator it = array.iterator(),运行结果就是Iterator接口的实现类的对象
  20. * it是接口的实现类对象,调用方法 hasNext 和 next 集合元素迭代
  21. */
  22. public class IteratorDemo {
  23. public static void main(String[] args) {
  24. Collection<String> coll = new ArrayList<String>();
  25. coll.add("abc1");
  26. coll.add("abc2");
  27. coll.add("abc3");
  28. coll.add("abc4");
  29. //迭代器,对集合ArrayList中的元素进行取出
  30.  
  31. //调用集合的方法iterator()获取出,Iterator接口的实现类的对象
  32. Iterator<String> it = coll.iterator();
  33. //接口实现类对象,调用方法hasNext()判断集合中是否有元素
  34. //boolean b = it.hasNext();
  35. //System.out.println(b);
  36. //接口的实现类对象,调用方法next()取出集合中的元素
  37. //String s = it.next();
  38. //System.out.println(s);
  39.  
  40. //迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
  41. while(it.hasNext()){
  42. String s = it.next();
  43. System.out.println(s);
  44. }
  45.  
  46. /*for (Iterator<String> it2 = coll.iterator(); it2.hasNext(); ) {
  47. System.out.println(it2.next());
  48. }*/
  49.  
  50. }
  51. }
  1. package cn.it.demo2;
  2. import java.util.ArrayList;
  3.  
  4. /*
  5. * JDK1.5新特性,增强for循环
  6. * JDK1.5版本后,出现新的接口 java.lang.Iterable
  7. * Collection开是继承Iterable
  8. * Iterable作用,实现增强for循环
  9. *
  10. * 格式:
  11. * for( 数据类型 变量名 : 数组或者集合 ){
  12. * sop(变量);
  13. * }
  14. */
  15. import cn.itcast.demo.Person;
  16. public class ForEachDemo {
  17. public static void main(String[] args) {
  18. function_2();
  19. }
  20. /*
  21. * 增强for循环遍历集合
  22. * 存储自定义Person类型
  23. */
  24. public static void function_2(){
  25. ArrayList<Person> array = new ArrayList<Person>();
  26. array.add(new Person("a",20));
  27. array.add(new Person("b",10));
  28. for(Person p : array){
  29. System.out.println(p);
  30. }
  31. }
  32.  
  33. public static void function_1(){
  34. //for对于对象数组遍历的时候,能否调用对象的方法呢
  35. String[] str = {"abc","itcast","cn"};
  36. for(String s : str){
  37. System.out.println(s.length());
  38. }
  39. }
  40.  
  41. /*
  42. * 实现for循环,遍历数组
  43. * 好处: 代码少了,方便对容器遍历
  44. * 弊端: 没有索引,不能操作容器里面的元素
  45. */
  46. public static void function(){
  47. int[] arr = {3,1,9,0};
  48. for(int i : arr){
  49. System.out.println(i+1);
  50. }
  51. System.out.println(arr[0]);
  52. }
  53. }
  1. package com.it.demo07_list;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. /*
  8. 案例; List集合入门.
  9.  
  10. 需求:
  11. 1.创建List集合, 用来存储字符串.
  12. 2.往List集合中添加4个字符串值, 分别是: "hello", "world", "java", "world"
  13. 3.通过迭代器, 遍历List集合, 获取每一个元素, 并打印到控制台上.
  14.  
  15. List集合简介:
  16. 概述:
  17. List是一个接口, 也是Collection接口的子接口, 它表示有序集合(也叫: 序列).
  18. 特点:
  19. 有序(元素的存,取顺序一致), 可重复, 元素有索引.
  20. */
  21. public class Demo01 {
  22. public static void main(String[] args) {
  23. //1. 创建集合对象.
  24. List<String> list = new ArrayList<>();
  25.  
  26. //2. 创建元素对象.
  27. //分解版
  28. /*String s1 = "hello";
  29. list.add(s1);*/
  30. //3. 把元素对象添加到集合中.
  31. list.add("hello");
  32. list.add("world");
  33. list.add("world");
  34. list.add("java");
  35.  
  36. //4. 遍历集合.
  37. //4.1 根据集合对象获取其对应的迭代器对象. Collection#iterator()
  38. Iterator<String> it = list.iterator();
  39. //4.2 判断迭代器中是否有下一个元素. Iterator#hasNext()
  40. while (it.hasNext()) {
  41. String s = it.next();
  42. //4.3 如果有, 则获取下一个元素. Iterator#next();
  43. System.out.println(s);
  44. }
  45. }
  46. }
  1. package com.it.demo07_list;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*
  7. 案例: 演示List接口的独有成员方法, 主要都是针对于 索引 操作的.
  8.  
  9. 涉及到的List集合中的成员方法:
  10. public void add(int index, E element); 往指定索引处插入指定的元素.
  11. public E remove(int index); 根据索引, 移除其对应的数据, 并返回该数据.
  12. public E set(int index, E element); 修改指定索引处的元素为指定的值.
  13. public E get(int index); 根据索引, 获取其对应的元素.
  14.  
  15. 细节:
  16. 上述四个方法的相同点是, 如果索引越界会报: 索引越界异常(IndexOutOfBoundsException).
  17.  
  18. 需求:
  19. 1.创建List集合, 用来存储字符串.
  20. 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
  21. 3.演示上述的4个方法.
  22. */
  23. public class Demo02 {
  24. public static void main(String[] args) {
  25. //1. 创建集合对象.
  26. List<String> list = new ArrayList<>();
  27.  
  28. //2. 创建元素对象.
  29. //3. 把元素对象添加到集合中.
  30. list.add("hello");
  31. list.add("world");
  32. list.add("java");
  33.  
  34. //4. 测试上述的4个成员方法
  35. //测试: public void add(int index, E element); 往指定索引处插入指定的元素.
  36. //list.add(2, "hadoop"); //hello world hadoop java
  37. //list.add(3, "hadoop"); //hello world java hadoop
  38. //list.add(4, "hadoop"); //报错, 索引越界异常.
  39.  
  40. //测试: public E remove(int index); 根据索引, 移除其对应的数据, 并返回该数据.
  41. //System.out.println(list.remove(1)); //world
  42. //System.out.println(list.remove(10)); //报错, 索引越界异常.
  43.  
  44. //测试: public E set(int index, E element); 修改指定索引处的元素为指定的值.
  45. //System.out.println(list.set(1, "hadoop")); //world
  46. //System.out.println(list.set(3, "hadoop")); //报错, 索引越界异常.
  47.  
  48. //测试: public E get(int index); 根据索引, 获取其对应的元素.
  49. System.out.println(list.get(0));
  50. System.out.println(list.get(1));
  51. System.out.println(list.get(2));
  52.  
  53. //5. 打印集合.
  54. System.out.println(list);
  55. }
  56. }
  1. package com.it.demo07_list;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*
  7. 案例: 演示泛型类, 内容能看懂就行.
  8. */
  9. public class Demo03<HG> {
  10. public static void main(String[] args) {
  11. //需求: 调用Demo03#show();
  12. //泛型类: 在创建对象的时候, 明确具体的泛型.
  13. Demo03<String> d1 = new Demo03<>();
  14. d1.show("abc");
  15. //d1.show(10); //报错.
  16.  
  17. Demo03<Integer> d2 = new Demo03<>();
  18. d2.show(10);
  19. //d2.show("abc"); //报错
  20.  
  21. List<Integer> list = new ArrayList<>();
  22. list.add(10);
  23. //list.add("abc"); //报错
  24. }
  25.  
  26. public void show(HG e) {
  27. System.out.println(e);
  28. }
  29. }
  1. package com.it.demo07_list;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*
  7. 案例: 演示List集合独有的遍历方式.
  8.  
  9. 需求:
  10. 1.创建List集合, 用来存储字符串.
  11. 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
  12. 3.通过for循环 + size() + get()的形式, 遍历List集合.
  13. */
  14. public class Demo04 {
  15. public static void main(String[] args) {
  16. //1. 创建集合对象.
  17. List<String> list = new ArrayList<>();
  18.  
  19. //2. 创建元素对象.
  20. //3. 把元素对象添加到集合中.
  21. list.add("hello");
  22. list.add("world");
  23. list.add("java");
  24.  
  25. //4. 遍历集合. 普通for方式.
  26. for (int i = 0; i < list.size(); i++) {
  27. //根据索引, 获取集合中的元素.
  28. String s = list.get(i);
  29. System.out.println(s);
  30. }
  31. System.out.println("--------------------------");
  32. //通过普通for循环, 遍历List集合的快捷键: Iterator Array: itar(遍历数组), Iterator List: itli(遍历集合)
  33. for (int i = 0; i < list.size(); i++) {
  34. String s = list.get(i);
  35. System.out.println(s);
  36. }
  37.  
  38. }
  39. }
  1. package com.it.pojo;
  2.  
  3. //自定义的JavaBean类, 表示学生
  4. public class Student {
  5. //属性: 姓名和年龄
  6. private String name;
  7. private int age;
  8.  
  9. public Student() {
  10. }
  11.  
  12. public Student(String name, int age) {
  13. this.name = name;
  14. this.age = age;
  15. }
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. public int getAge() {
  26. return age;
  27. }
  28.  
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32.  
  33. @Override
  34. public String toString() {
  35. return "Student{" +
  36. "name='" + name + '\'' +
  37. ", age=" + age +
  38. '}';
  39. }
  40. }
  41.  
  42. package com.it.demo04_exercise;
  43.  
  44. import com.it.pojo.Student;
  45.  
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.Iterator;
  49.  
  50. /*
  51. 案例: 演示Collection集合存储 自定义对象并遍历.
  52.  
  53. 需求:
  54. 1.定义一个学生类, 属性为姓名和年龄.
  55. 2.创建Collection集合, 用来存储学生对象.
  56. 3.往Collection集合中, 添加3个学生的信息.
  57. 4.通过迭代器, 遍历集合.
  58. */
  59. public class Demo02 {
  60. public static void main(String[] args) {
  61. //1. 创建集合对象.
  62. Collection<Student> list = new ArrayList<>();
  63. //2. 创建元素对象.
  64. Student s1 = new Student("刘亦菲", 33);
  65. Student s2 = new Student("赵丽颖", 31);
  66. Student s3 = new Student("高圆圆", 35);
  67. Student s4 = new Student("王丽坤", 29);
  68. //3. 把元素对象添加到集合中.
  69. list.add(s1);
  70. list.add(s2);
  71. list.add(s3);
  72. list.add(s4);
  73.  
  74. //4. 遍历集合.
  75. //4.1 根据集合对象获取其对应的迭代器对象.
  76. Iterator<Student> it = list.iterator();
  77. //4.2 判断迭代器中是否有下一个元素.
  78. while (it.hasNext()) {
  79. //4.3 如果有, 就获取下一个元素, 并输出.
  80. Student stu = it.next();
  81. System.out.println(stu);
  82. }
  83. }
  84. }
  85. package com.it.demo04_exercise;
  86.  
  87. import com.it.pojo.Student;
  88.  
  89. import java.util.ArrayList;
  90. import java.util.Collection;
  91. import java.util.Iterator;
  92. import java.util.List;
  93.  
  94. /*
  95. 案例: 演示List集合存储自定义对象并遍历.
  96.  
  97. 需求:
  98. 1.定义一个学生类, 属性为姓名和年龄.
  99. 2.创建List集合, 用来存储学生对象.
  100. 3.往List集合中, 添加3个学生的信息.
  101. 4.分别通过两种遍历方式, 来遍历List集合.
  102.  
  103. 演示List集合的两种遍历方式:
  104. 方式一: 迭代器.
  105. 方式二: List体系独有的(普通for + get() + size())
  106. */
  107. public class Demo03 {
  108. public static void main(String[] args) {
  109. //1. 创建集合对象.
  110. List<Student> list = new ArrayList<>();
  111. //2. 创建元素对象.
  112. Student s1 = new Student("刘亦菲", 33);
  113. Student s2 = new Student("赵丽颖", 31);
  114. Student s3 = new Student("高圆圆", 35);
  115. Student s4 = new Student("王丽坤", 29);
  116. //3. 把元素对象添加到集合中.
  117. list.add(s1);
  118. list.add(s2);
  119. list.add(s3);
  120. list.add(s4);
  121.  
  122. //4. 遍历集合.
  123. //方式一: 普通迭代器.
  124. Iterator<Student> it = list.iterator();
  125. while (it.hasNext()) {
  126. //分解版
  127. /*Student stu = it.next();
  128. System.out.println(stu);*/
  129.  
  130. //合并版, 正确的写法
  131. System.out.println(it.next());
  132.  
  133. //合并版, 错误写法, 只能: hasNext()判断一次, next()获取一次.
  134. //System.out.println(it.next().getName() + "..." + it.next().getAge()); //刘亦菲...33
  135. }
  136. System.out.println("-------------------");
  137.  
  138. //方式二: 普通for循环.
  139. for (int i = 0; i < list.size(); i++) {
  140. Student stu = list.get(i);
  141. System.out.println(stu);
  142. }
  143. }
  144. }
  1. package cn.it.demo3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6. /*
  7. * JDK1.5 出现新的安全机制,保证程序的安全性
  8. * 泛型: 指明了集合中存储数据的类型 <数据类型>
  9. */
  10.  
  11. public class GenericDemo {
  12. public static void main(String[] args) {
  13. function();
  14. }
  15.  
  16. public static void function(){
  17. Collection<String> coll = new ArrayList<String>();
  18. coll.add("abc");
  19. coll.add("rtyg");
  20. coll.add("43rt5yhju");
  21. // coll.add(1);
  22.  
  23. Iterator<String> it = coll.iterator();
  24. while(it.hasNext()){
  25. String s = it.next();
  26. System.out.println(s.length());
  27. }
  28. }
  29. }
  1. package cn.it.demo3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. /*
  7. * 带有泛型的类
  8. * ArrayList
  9. * E: Element 元素, 实际思想就是一个变量而已
  10. * ArrayList<Integer> , E 接受到类型,就是Integer类型
  11. * public class ArrayList<E>{
  12. *
  13. * public boolean add(Integer e){
  14. * elementData[size++] = e;
  15. * }
  16. *
  17. * public boolean add(E e){}
  18. * }
  19. *
  20. * Iterator<E>
  21. * E next()
  22. *
  23. * Iterator<Integer>
  24. * Integer next()
  25. *
  26. */
  27. public class GenericDemo1 {
  28. public static void main(String[] args) {
  29. ArrayList<Integer> array = new ArrayList<Integer> ();
  30. array.add(123);
  31. array.add(456);
  32. // ArrayList集合,自己有个方法
  33. // <T> T[] toArray(T[] a)
  34. Integer[] i = new Integer[array.size()];
  35. Integer [] j = array.toArray(i);
  36. for(Integer k : j){
  37. System.out.println(k);
  38. }
  39.  
  40. }
  41. }
  1. package cn.it.demo3;
  2. /*
  3. * 带有泛型的接口
  4. *
  5. * public interface List <E>{
  6. * abstract boolean add(E e);
  7. * }
  8. * 实现类,先实现接口,不理会泛型
  9. * public class ArrayList<E> implements List<E>{
  10. * }
  11. * 调用者 : new ArrayList<String>() 后期创建集合对象的时候,指定数据类型
  12. *
  13. * 实现类,实现接口的同时,也指定了数据类型
  14. * public class XXX implements List<String>{
  15. * }
  16. * new XXX()
  17. */
  18. public class GenericDemo2 {
  19.  
  20. }
  1. package cn.it.demo4;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7.  
  8. /*
  9. * 泛型的通配符
  10. */
  11. public class GenericDemo {
  12. public static void main(String[] args) {
  13. ArrayList<String> array = new ArrayList<String>();
  14.  
  15. HashSet<Integer> set = new HashSet<Integer>();
  16.  
  17. array.add("123");
  18. array.add("456");
  19.  
  20. set.add(789);
  21. set.add(890);
  22.  
  23. iterator(array);
  24. iterator(set);
  25. }
  26. /*
  27. * 定义方法,可以同时迭代2个集合
  28. * 参数: 怎么实现 , 不能写ArrayList,也不能写HashSet
  29. * 参数: 或者共同实现的接口
  30. * 泛型的通配,匹配所有的数据类型 ?
  31. */
  32. public static void iterator(Collection<?> coll){
  33. Iterator<?> it = coll.iterator();
  34. while(it.hasNext()){
  35. //it.next()获取的对象,什么类型
  36. System.out.println(it.next());
  37. }
  38. }
  39. }
  1. package cn.it.hotel;
  2. /*
  3. * 酒店的VIP服务
  4. * 厨师和服务员
  5. */
  6. public interface VIP {
  7. public abstract void services();
  8. }
  9.  
  10. package cn.it.hotel;
  11. /*
  12. * 酒店的员工类
  13. * 员工共性, 姓名,工号 工作方法
  14. */
  15. public abstract class Employee {
  16. private String name;
  17. private String id;
  18.  
  19. public Employee(){}
  20.  
  21. public Employee(String name,String id){
  22. this.name = name;
  23. this.id = id;
  24. }
  25.  
  26. public abstract void work();
  27.  
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getId() {
  35. return id;
  36. }
  37. public void setId(String id) {
  38. this.id = id;
  39. }
  40.  
  41. }
  42.  
  43. package cn.it.hotel;
  44. /*
  45. * 定义厨师类
  46. * 属于员工一种,具有额外服务功能
  47. * 继承Employee,实现VIP接口
  48. */
  49. public class ChuShi extends Employee implements VIP{
  50. //空参数构造方法
  51. public ChuShi(){}
  52.  
  53. //有参数构造方法
  54. public ChuShi(String name,String id){
  55. super(name,id);
  56. }
  57.  
  58. //抽象方法重写
  59. public void work(){
  60. System.out.println("厨师在炒菜");
  61. }
  62. public void services(){
  63. System.out.println("厨师做菜加量");
  64. }
  65. }
  1. package cn.it.hotel;
  2. /*
  3. * 定义厨师类
  4. * 属于员工一种,具有额外服务功能
  5. * 继承Employee,实现VIP接口
  6. */
  7. public class FuWuYuan extends Employee implements VIP{
  8. //空参数构造方法
  9. public FuWuYuan() {
  10. super();
  11.  
  12. }
  13. //满参数构造方法
  14. public FuWuYuan(String name, String id) {
  15. super(name, id);
  16.  
  17. }
  18.  
  19. //抽象方法重写
  20. public void work(){
  21. System.out.println("服务员在上菜");
  22. }
  23.  
  24. public void services(){
  25. System.out.println("服务员给顾客倒酒");
  26. }
  27. }
  1.  

  

  1. package cn.it.hotel;
  2. /*
  3. * 定义经理类
  4. * 属于员工一种,没有VIP功能
  5. * 自己有奖金属性
  6. */
  7. public class JingLi extends Employee {
  8. //空参数构造方法
  9. public JingLi(){}
  10.  
  11. //满参数构造方法
  12. public JingLi(String name,String id,double money){
  13. super(name, id);
  14. this.money = money;
  15. }
  16.  
  17. //定义奖金属性
  18. private double money;
  19.  
  20. public double getMoney() {
  21. return money;
  22. }
  23.  
  24. public void setMoney(double money) {
  25. this.money = money;
  26. }
  27.  
  28. //重写抽象方法
  29. public void work(){
  30. System.out.println("管理,谁出错我罚谁");
  31. }
  32. }

 

  1. package cn.it.hotel;
  2.  
  3. public class Test {
  4. public static void main(String[] args) {
  5. //创建1个经理,2个服务员,2个厨师
  6. JingLi jl = new JingLi("小名", "董事会001", 123456789.32);
  7. jl.work();
  8.  
  9. FuWuYuan f1 = new FuWuYuan("翠花", "服务部001");
  10. FuWuYuan f2 = new FuWuYuan("酸菜", "服务部002");
  11.  
  12. f1.work();
  13. f1.services();
  14.  
  15. f2.work();
  16. f2.services();
  17.  
  18. ChuShi c1 = new ChuShi("老干妈", "后厨001");
  19. ChuShi c2 = new ChuShi("老干爹", "后厨002");
  20.  
  21. c1.work();
  22. c1.services();
  23.  
  24. c2.work();
  25. c2.services();
  26.  
  27. }
  28. }
  1. package cn.it.hotel;
  2. /*
  3. * 将的酒店员工,厨师,服务员,经理,分别存储到3个集合中
  4. * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法
  5. */
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. public class GenericTest {
  9. public static void main(String[] args) {
  10. //创建3个集合对象
  11. ArrayList<ChuShi> cs = new ArrayList<ChuShi>();
  12. ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>();
  13. ArrayList<JingLi> jl = new ArrayList<JingLi>();
  14.  
  15. //每个集合存储自己的元素
  16. cs.add(new ChuShi("张三", "后厨001"));
  17. cs.add(new ChuShi("李四", "后厨002"));
  18.  
  19. fwy.add(new FuWuYuan("翠花", "服务部001"));
  20. fwy.add(new FuWuYuan("酸菜", "服务部002"));
  21.  
  22. jl.add(new JingLi("小名", "董事会001", 123456789.32));
  23. jl.add(new JingLi("小强", "董事会002", 123456789.33));
  24.  
  25. // ArrayList<String> arrayString = new ArrayList<String>();
  26. iterator(jl);
  27. iterator(fwy);
  28. iterator(cs);
  29.  
  30. }
  31. /*
  32. * 定义方法,可以同时遍历3集合,遍历三个集合的同时,可以调用工作方法 work
  33. * ? 通配符,迭代器it.next()方法取出来的是Object类型,怎么调用work方法
  34. * 强制转换: it.next()=Object o ==> Employee
  35. * 方法参数: 控制,可以传递Employee对象,也可以传递Employee的子类的对象
  36. * 泛型的限定 本案例,父类固定Employee,但是子类可以无限?
  37. * ? extends Employee 限制的是父类, 上限限定, 可以传递Employee,传递他的子类对象
  38. * ? super Employee 限制的是子类, 下限限定, 可以传递Employee,传递他的父类对象
  39. */
  40. public static void iterator(ArrayList<? extends Employee> array){
  41.  
  42. Iterator<? extends Employee> it = array.iterator();
  43. while(it.hasNext()){
  44. //获取出的next() 数据类型,是什么Employee
  45. Employee e = it.next();
  46. e.work();
  47. }
  48. }
  49. }
  1. package cn.it.demo;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. /*
  6. * LinkedList 链表集合的特有功能
  7. * 自身特点: 链表底层实现,查询慢,增删快
  8. *
  9. * 子类的特有功能,不能多态调用
  10. */
  11. public class LinkedListDemo {
  12. public static void main(String[] args) {
  13. function_3();
  14. }
  15. /*
  16. * E removeFirst() 移除并返回链表的开头
  17. * E removeLast() 移除并返回链表的结尾
  18. */
  19. public static void function_3(){
  20. LinkedList<String> link = new LinkedList<String>();
  21. link.add("1");
  22. link.add("2");
  23. link.add("3");
  24. link.add("4");
  25.  
  26. String first = link.removeFirst();
  27. String last = link.removeLast();
  28. System.out.println(first);
  29. System.out.println(last);
  30.  
  31. System.out.println(link);
  32. }
  33.  
  34. /*
  35. * E getFirst() 获取链表的开头
  36. * E getLast() 获取链表的结尾
  37. */
  38. public static void function_2(){
  39. LinkedList<String> link = new LinkedList<String>();
  40. link.add("1");
  41. link.add("2");
  42. link.add("3");
  43. link.add("4");
  44.  
  45. if(!link.isEmpty()){
  46. String first = link.getFirst();
  47. String last = link.getLast();
  48. System.out.println(first);
  49. System.out.println(last);
  50. }
  51. }
  52.  
  53. public static void function_1(){
  54. LinkedList<String> link = new LinkedList<String>();
  55. link.addLast("a");
  56. link.addLast("b");
  57. link.addLast("c");
  58. link.addLast("d");
  59.  
  60. link.addFirst("1");
  61. link.addFirst("2");
  62. link.addFirst("3");
  63. System.out.println(link);
  64. }
  65.  
  66. /*
  67. * addFirst(E) 添加到链表的开头
  68. * addLast(E) 添加到链表的结尾
  69. */
  70. public static void function(){
  71. LinkedList<String> link = new LinkedList<String>();
  72.  
  73. link.addLast("heima");
  74.  
  75. link.add("abc");
  76. link.add("bcd");
  77.  
  78. link.addFirst("itcast");
  79. System.out.println(link);
  80.  
  81. }
  82. }
  1. package cn.it.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*
  7. * List接口派系, 继承Collection接口
  8. * 下面有很多实现类
  9. * List接口特点: 有序,索引,可以重复元素
  10. * 实现类, ArrayList, LinkedList
  11. *
  12. * List接口中的抽象方法,有一部分方法和他的父接口Collection是一样
  13. * List接口的自己特有的方法, 带有索引的功能
  14. */
  15. public class ListDemo {
  16. public static void main(String[] args) {
  17. function_2();
  18. }
  19. /*
  20. * E set(int index, E)
  21. * 修改指定索引上的元素
  22. * 返回被修改之前的元素
  23. */
  24. public static void function_2(){
  25. List<Integer> list = new ArrayList<Integer>();
  26. list.add(1);
  27. list.add(2);
  28. list.add(3);
  29. list.add(4);
  30.  
  31. Integer i = list.set(0, 5);
  32. System.out.println(i);
  33. System.out.println(list);
  34. }
  35.  
  36. /*
  37. * E remove(int index)
  38. * 移除指定索引上的元素
  39. * 返回被删除之前的元素
  40. */
  41. public static void function_1(){
  42. List<Double> list = new ArrayList<Double>();
  43. list.add(1.1);
  44. list.add(1.2);
  45. list.add(1.3);
  46. list.add(1.4);
  47.  
  48. Double d = list.remove(0);
  49. System.out.println(d);
  50. System.out.println(list);
  51. }
  52.  
  53. /*
  54. * add(int index, E)
  55. * 将元素插入到列表的指定索引上
  56. * 带有索引的操作,防止越界问题
  57. * java.lang.IndexOutOfBoundsException
  58. * ArrayIndexOutOfBoundsException
  59. * StringIndexOutOfBoundsException
  60. */
  61. public static void function(){
  62. List<String> list = new ArrayList<String>();
  63. list.add("abc1");
  64. list.add("abc2");
  65. list.add("abc3");
  66. list.add("abc4");
  67. System.out.println(list);
  68.  
  69. list.add(1, "itcast");
  70. System.out.println(list);
  71. }
  72. }
  1. package cn.it.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. /*
  8. * 迭代器的并发修改异常 java.util.ConcurrentModificationException
  9. * 就是在遍历的过程中,使用了集合方法修改了集合的长度,不允许的
  10. */
  11. public class ListDemo1 {
  12. public static void main(String[] args) {
  13. List<String> list = new ArrayList<String>();
  14. list.add("abc1");
  15. list.add("abc2");
  16. list.add("abc3");
  17. list.add("abc4");
  18.  
  19. //对集合使用迭代器进行获取,获取时候判断集合中是否存在 "abc3"对象
  20. //如果有,添加一个元素 "ABC3"
  21. Iterator<String> it = list.iterator();
  22. while(it.hasNext()){
  23. String s = it.next();
  24. //对获取出的元素s,进行判断,是不是有"abc3"
  25. if(s.equals("abc3")){
  26. list.add("ABC3");
  27. }
  28. System.out.println(s);
  29. }
  30. }
  31. }
  1. package com.it.pojo;
  2.  
  3. public class Student {
  4. private String name;
  5. private int age;
  6.  
  7. public Student() {
  8. }
  9.  
  10. public Student(String name, int age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22.  
  23. public int getAge() {
  24. return age;
  25. }
  26.  
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30.  
  31. @Override
  32. public String toString() {
  33. return "Student{" +
  34. "name='" + name + '\'' +
  35. ", age=" + age +
  36. '}';
  37. }
  38.  
  39. //重写hashCode(), equals()方法
  40. @Override
  41. public boolean equals(Object o) {
  42. if (this == o) return true;
  43. if (o == null || getClass() != o.getClass()) return false;
  44.  
  45. Student student = (Student) o;
  46.  
  47. if (age != student.age) return false;
  48. return name != null ? name.equals(student.name) : student.name == null;
  49. }
  50.  
  51. @Override
  52. public int hashCode() {
  53. int result = name != null ? name.hashCode() : 0;
  54. result = 31 * result + age;
  55. return result;
  56. }
  57.  
  58. /* @Override
  59. public int hashCode() {
  60. int result = name != null ? name.hashCode() : 0;
  61. result = 31 * result + age; //12: 0, 12, 1, 11 2,10 3, 9
  62. return result;
  63. //return 1; //我们可以实现, 对象的内容不同, 但是哈希值相同这种情况, 你知道可以这样写就行了, 实际开发几乎不用.
  64. }*/
  65. }
  66.  
  67. package com.it.demo01_list;
  68.  
  69. import com.it.pojo.Student;
  70.  
  71. import java.util.ArrayList;
  72. import java.util.Iterator;
  73. import java.util.List;
  74.  
  75. //案例: 演示List集合存储自定义对象并遍历.
  76. /*
  77. 使用集合的步骤:
  78. 1. 创建集合对象.
  79. 2. 创建元素对象.
  80. 3. 把元素对象添加到集合中.
  81. 4. 遍历集合.
  82. A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
  83. B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
  84. C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
  85.  
  86. 一些快捷键:
  87. 迭代器遍历的快捷键: itit iterator iterator
  88. 普通for循环遍历数组: itar iterator array
  89. 普通for循环遍历集合: itli iterator list
  90. 增强for: iter iterator
  91. */
  92. public class Demo01 {
  93. public static void main(String[] args) {
  94. //1. 创建集合对象.
  95. List<Student> list = new ArrayList<>();
  96. //2. 创建元素对象.
  97. Student s1 = new Student("刘亦菲", 33);
  98. Student s2 = new Student("赵丽颖", 35);
  99. Student s3 = new Student("高圆圆", 31);
  100. //3. 把元素对象添加到集合中.
  101. list.add(s1);
  102. list.add(s2);
  103. list.add(s3);
  104. //4. 遍历集合.
  105. //方式一: 普通迭代器
  106. //A. 根据集合对象获取其对应的迭代器对象. //根据仓库找到该仓库的 仓库管理员.
  107. Iterator<Student> it = list.iterator();
  108. //B. 判断迭代器中是否有下一个元素. //仓库管理员查阅数据, 仓库中有无 库存.
  109. while (it.hasNext()) {
  110. //C. 如果有, 就获取元素. //如果仓库有库存, 我们就取出这个货物.
  111. Student student = it.next();
  112. System.out.println(student);
  113. }
  114. System.out.println("----------------------");
  115.  
  116. //方式二: 普通for循环.
  117. for (int i = 0; i < list.size(); i++) {
  118. Student student = list.get(i);
  119. System.out.println(student);
  120. }
  121. }
  122. }

  1. package com.it.demo01_list;
  2.  
  3. import com.it.pojo.Student;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.ListIterator;
  8.  
  9. /*
  10. 案例: 演示列表迭代器入门.
  11.  
  12. 需求:
  13. 1.创建List集合, 用来存储字符串.
  14. 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
  15. 3.通过列表迭代器对List集合分别进行正向遍历和逆向遍历.
  16.  
  17. List接口中的成员方法:
  18. public ListIterator<E> listIteator(); 根据List集合, 获取其对应的列表迭代器.
  19. ListIterator中的成员方法:
  20. public boolean hasNext(); 从Iterator迭代器中继承过来的, 判断是否有下一个元素.
  21. public E next(); 从Iterator迭代器中继承过来的, 获取下一个元素.
  22.  
  23. public boolean hasPrevious(); ListIterator迭代器独有的成员方法, 判断是否有上一个元素.
  24. public E previous(); ListIterator迭代器独有的成员方法, 获取上一个元素.
  25.  
  26. 细节(记忆):
  27. 1. 列表迭代器(ListIterator) 它是List体系独有的迭代器.
  28. 2. 使用列表迭代器进行逆向遍历之前, 必须通过该迭代器先进行一次正向遍历.
  29. */
  30. public class Demo02 {
  31. public static void main(String[] args) {
  32. //需求: 演示列表迭代器.
  33. //1. 创建集合对象.
  34. List<String> list = new ArrayList<>();
  35. //2. 创建元素对象.
  36. //3. 把元素对象添加到集合中.
  37. list.add("hello");
  38. list.add("world");
  39. list.add("java");
  40.  
  41. //4. 遍历集合.
  42. //通过ListIterator列表迭代器实现: 集合元素的 正向遍历, 即: 从前往后.
  43. //获取列表迭代器对象.
  44. ListIterator<String> lit = list.listIterator();
  45. //判断是否有下一个元素
  46. while (lit.hasNext()) {
  47. //如果有, 就获取下一个元素
  48. String s = lit.next();
  49. //打印结果
  50. System.out.println(s);
  51. }
  52. System.out.println("-------------------------------");
  53.  
  54. //通过ListIterator列表迭代器实现: 集合元素的 逆向遍历, 即: 从后往前.
  55. //判断是否有上一个元素
  56. while (lit.hasPrevious()) {
  57. //如果有, 就获取上一个元素
  58. String s = lit.previous();
  59. //打印结果
  60. System.out.println(s);
  61. }
  62.  
  63. }
  64. }
  1. package com.it.demo01_list;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7.  
  8. /*
  9. 案例: 演示并发修改异常.
  10.  
  11. 需求:
  12. 1.创建List集合, 用来存储字符串.
  13. 2.往List集合中添加3个字符串值, 分别是: "hello", "world", "java".
  14. 3.判断集合中是否有"world"元素, 如果有, 就往集合中添加一个"JavaEE".
  15.  
  16. 并发修改异常简介:
  17. 问题描述:
  18. 并发修改异常也叫ConcurrentModificationException, 指的是我们在使用 普通迭代器遍历集合的同时, 又往集合中添加元素了, 就会出现此问题.
  19. //大白话翻译: 我找你帮我打扫3层楼的卫生, 我给你1000块钱, 当你打扫到第2层的时候, 我说你只有打扫完整个4层楼的卫生, 我才给你1000, 你肯定不干.
  20. 产生原因:
  21. 当我们通过集合对象获取迭代器对象的时候, 迭代器内部会有一个变量记录此时集合中的元素个数, 如果实际要遍历的元素比这个值大, 就会报并发修改异常.
  22. 解决方案:
  23. 1. 通过普通for遍历List集合, 然后添加元素即可. 这种方式添加的元素, 是在整个集合元素的最后的.
  24. 2. 通过列表迭代器来遍历集合, 并且要通过列表迭代器的add()方法添加元素, 这种方式添加的元素, 只在指定元素的元素后添加的.
  25. //大白话翻译: 你是清洁工, 你比较好说话, 我们谈好的是你打扫3层, 我给你1000, 但是你看我4楼也比较脏, 你说义务帮我打扫.
  26. 3. 采用CopyOnWriteArrayList集合实现, 该集合能自动处理并发修改异常.
  27. */
  28. public class Demo03 {
  29. public static void main(String[] args) {
  30. //需求: 演示列表迭代器.
  31. //1. 创建集合对象.
  32. List<String> list = new ArrayList<>();
  33. //2. 创建元素对象.
  34. //3. 把元素对象添加到集合中.
  35. list.add("hello");
  36. list.add("world");
  37. list.add("java");
  38. //4. 遍历集合.
  39.  
  40. //普通的列表迭代器, 会产生并发修改异常.
  41. /* Iterator<String> it = list.iterator();
  42. while (it.hasNext()) {
  43. String s = it.next();
  44. if ("world".equals(s)){
  45. //如果当前元素是"world", 就往集合中添加一个"JavaEE".
  46. list.add("JavaEE");
  47. }
  48. }*/
  49.  
  50. //解决方案一: 通过普通for遍历List集合,
  51. /*for (int i = 0; i < list.size(); i++) {
  52. String s = list.get(i);
  53. if ("world".equals(s)) {
  54. //如果当前元素是"world", 就往集合中添加一个"JavaEE".
  55. list.add("JavaEE");
  56. }
  57. }*/
  58.  
  59. //解决方案二: 通过列表迭代器来遍历集合,
  60. /*ListIterator<String> lit = list.listIterator();
  61. while (lit.hasNext()) {
  62. String s = lit.next();
  63. if ("world".equals(s)) {
  64. //如果当前元素是"world", 就往集合中添加一个"JavaEE".
  65. lit.add("JavaEE"); //细节: 调用迭代器的添加元素的方法
  66. }
  67. }*/
  68.  
  69. //5. 打印集合
  70. System.out.println("list: " + list);
  71. }
  72. }
  1. package com.it.demo01_list;
  2.  
  3. import java.util.Iterator;
  4. import java.util.concurrent.CopyOnWriteArrayList;
  5.  
  6. //案例: 演示CopyOnWriteArrayList集合, 解决并发修改异常.
  7. public class Demo04 {
  8. public static void main(String[] args) {
  9. //需求: 演示列表迭代器.
  10. //1. 创建集合对象.
  11. CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
  12. //2. 创建元素对象.
  13. //3. 把元素对象添加到集合中.
  14. list.add("hello");
  15. list.add("world");
  16. list.add("java");
  17.  
  18. //4. 遍历集合.
  19. Iterator<String> it = list.iterator();
  20. while (it.hasNext()) {
  21. String s = it.next();
  22. if ("world".equals(s)){
  23. //如果当前元素是"world", 就往集合中添加一个"JavaEE".
  24. list.add("JavaEE");
  25. }
  26. }
  27.  
  28. //5. 打印集合
  29. System.out.println(list);
  30. }
  31. }

  

  1.  
  1. package com.it.demo02_for;
  2.  
  3. /*
  4. 案例: 演示增强for遍历数组.
  5.  
  6. 需求:
  7. 1.定义int类型的数组, 存储元素1, 2, 3, 4, 5.
  8. 2.通过增强for, 遍历上述的数组.
  9.  
  10. 增强for简介:
  11. 概述:
  12. 它是JDK1.5的特性, 是帮助我们简化遍历数组或者集合动作的.
  13. 格式:
  14. for(元素的数据类型 变量名 : 要遍历的集合或者数组) {
  15. //正常的逻辑代码
  16. }
  17. 快捷键:
  18. iter
  19. 本质:
  20. 增强for的底层就是迭代器实现的, 换言之: 增强for封装的就是迭代器.
  21. */
  22. public class Demo01 {
  23. public static void main(String[] args) {
  24. //1. 定义int数组
  25. int[] arr = {1, 2, 3, 4, 5};
  26. //2. 通过增强for遍历int数组.
  27. for (int num : arr) {
  28. System.out.println(num);
  29. }
  30. }
  31. }
  1. package com.it.demo02_for;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. //案例: 演示增强for出现并发修改异常, 说明: 增强for的底层就是一个普通的迭代器.
  7. public class Demo02 {
  8. public static void main(String[] args) {
  9. //1. 创建集合对象.
  10. List<String> list = new ArrayList<>();
  11. //2. 创建元素对象.
  12. //3. 把元素对象添加到集合中.
  13. list.add("hello");
  14. list.add("world");
  15. list.add("java");
  16.  
  17. //4.通过增强for遍历上述的集合对象.
  18. for (String s : list) {
  19. //5.判断List集合中是否有"world", 如果有, 就往集合中添加元素"JavaEE".
  20. if ("world".equals(s)) {
  21. //如果当前元素是"world", 就往集合中添加一个"JavaEE".
  22. list.add("JavaEE");
  23. }
  24. }
  25.  
  26. //6. 打印集合
  27. System.out.println("list: " + list);
  28. }
  29. }
  1. package com.it.demo03_exercise;
  2.  
  3. import com.it.pojo.Student;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.ListIterator;
  9.  
  10. /*
  11. 案例: List集合存储学生对象, 并遍历. 4种方式实现.
  12.  
  13. 遍历方式:
  14. 方式一: 增强for
  15. 方式二: 列表迭代器.
  16. 方式三: 普通for循环.
  17. 方式四: 普通迭代器.
  18. */
  19. public class Demo01 {
  20. public static void main(String[] args) {
  21. //1. 创建集合对象.
  22. //多态
  23. List<Student> list = new ArrayList<>();
  24. //2. 创建元素对象.
  25. Student s1 = new Student("刘亦菲", 33);
  26. Student s2 = new Student("赵丽颖", 35);
  27. Student s3 = new Student("高圆圆", 31);
  28. //3. 把元素对象添加到集合中.
  29. list.add(s1);
  30. list.add(s2);
  31. list.add(s3);
  32. //4. 遍历集合.
  33. //方式一: 增强for
  34. for (Student student : list) {
  35. System.out.println(student);
  36. }
  37. System.out.println("----------------------");
  38.  
  39. //方式二: 列表迭代器.
  40. ListIterator<Student> lit = list.listIterator();
  41. while (lit.hasNext()) {
  42. Student student = lit.next();
  43. System.out.println(student);
  44. }
  45. System.out.println("----------------------");
  46.  
  47. //方式三: 普通for循环.
  48. for (int i = 0; i < list.size(); i++) {
  49. Student student = list.get(i);
  50. System.out.println(student);
  51. }
  52. System.out.println("----------------------");
  53.  
  54. //方式四: 普通迭代器.
  55. Iterator<Student> it = list.iterator();
  56. while (it.hasNext()) {
  57. Student student = it.next();
  58. System.out.println(student);
  59. }
  60. }
  61. }
  1. package com.it.demo03_exercise;
  2.  
  3. import com.it.pojo.Student;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.ListIterator;
  9.  
  10. /*
  11. 案例: ArrayList集合存储学生对象, 并遍历. 4种方式实现.
  12.  
  13. 遍历方式:
  14. 方式一: 增强for
  15. 方式二: 列表迭代器.
  16. 方式三: 普通for循环.
  17. 方式四: 普通迭代器.
  18. */
  19. public class Demo02 {
  20. public static void main(String[] args) {
  21. //1. 创建集合对象.
  22. ArrayList<Student> list = new ArrayList<>();
  23. //2. 创建元素对象.
  24. Student s1 = new Student("刘亦菲", 33);
  25. Student s2 = new Student("赵丽颖", 35);
  26. Student s3 = new Student("高圆圆", 31);
  27. //3. 把元素对象添加到集合中.
  28. list.add(s1);
  29. list.add(s2);
  30. list.add(s3);
  31. //4. 遍历集合.
  32. //方式一: 增强for
  33. for (Student student : list) {
  34. System.out.println(student);
  35. }
  36. System.out.println("----------------------");
  37.  
  38. //方式二: 列表迭代器.
  39. ListIterator<Student> lit = list.listIterator();
  40. while (lit.hasNext()) {
  41. Student student = lit.next();
  42. System.out.println(student);
  43. }
  44. System.out.println("----------------------");
  45.  
  46. //方式三: 普通for循环.
  47. for (int i = 0; i < list.size(); i++) {
  48. Student student = list.get(i);
  49. System.out.println(student);
  50. }
  51. System.out.println("----------------------");
  52.  
  53. //方式四: 普通迭代器.
  54. Iterator<Student> it = list.iterator();
  55. while (it.hasNext()) {
  56. Student student = it.next();
  57. System.out.println(student);
  58. }
  59. }
  60. }
  1. package com.it.demo04_linkedlist;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. /*
  6. 案例: 演示LinkedList集合入门.
  7.  
  8. 需求:
  9. 1.创建LinkedList集合对象, 存储字符串数据: "hello", "world", "java"
  10. 2.遍历LinkedList集合.
  11.  
  12. 关于List体系的子类简介:
  13. 它的常用子类有ArrayList和LinkedList两个, 特点如下:
  14. ArrayList: 底层数据结构是数组, 所以查询快, 增删相对较慢.
  15. LinkedList: 底层数据结构是链表, 所以增删快, 查询相对较慢.
  16. 它们的相同点是: 元素有序, 可重复, 元素有索引, 因为都属于List体系.
  17. */
  18. public class Demo01 {
  19. public static void main(String[] args) {
  20. //1. 创建集合.
  21. LinkedList<String> list = new LinkedList<>();
  22. //2. 添加元素.
  23. list.add("hello");
  24. list.add("world");
  25. list.add("java");
  26. //3. 遍历, 有4种方式, 这里我只写一种, 其他的自己补.
  27. for (String s : list) {
  28. System.out.println(s);
  29. }
  30. }
  31. }
  1. package com.it.demo04_linkedlist;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. /*
  6. 案例: 演示LinkedList集合的特有成员方法.
  7.  
  8. 需求:
  9. 1.创建LinkedList集合对象, 存储字符串数据: "hello", "world", "java"
  10. 2.分别演示上述的6个方法.
  11.  
  12. LinkedList简介:
  13. 它主要是操作集合的头和尾元素的, 所以里边也定义了大量的这类方法.
  14. 常用的成员方法如下:
  15. public void addFirst(E e) 往列表的开头插入指定的元素
  16. public void addLast(E e) 往列表的末尾插入指定的元素
  17. public E removeFirst() 删除列表中的第一个元素, 并返回被删除的元素
  18. public E removeLast() 删除列表中的最后一个元素, 并返回被删除的元素.
  19. public E getFirst() 返回列表的第一个元素
  20. public E getLast() 返回列表的最后一个元素
  21. */
  22. public class Demo02 {
  23. public static void main(String[] args) {
  24. //1. 创建集合.
  25. LinkedList<String> list = new LinkedList<>();
  26. //2. 添加元素.
  27. list.add("hello");
  28. list.add("world");
  29. list.add("java");
  30.  
  31. //3. 测试方法
  32. //测试添加
  33. //list.addFirst("hadoop");
  34. //list.addLast("linux"); //等价于: list.add("linux");
  35.  
  36. //测试删除
  37. //System.out.println(list.removeFirst()); //hello
  38. //System.out.println(list.removeLast()); //java 等价于: list.remove("java");
  39.  
  40. //测试获取
  41. System.out.println(list.getFirst()); //hello
  42. System.out.println(list.getLast()); //java
  43.  
  44. //4. 打印集合.
  45. System.out.println("list: " + list);
  46. }
  47. }
  1. package com.it.demo05_set;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. /*
  8. 案例: 演示Set集合入门.
  9.  
  10. 需求:
  11. 1.创建Set集合对象, 存储字符串数据: "hello", "world", "java", "world"
  12. 2.通过两种方式, 遍历Set集合.
  13.  
  14. Set集合简介:
  15. 概述:
  16. 它属于Collection集合(单列集合)的子体系, 它是一个接口, 所以不能直接new,
  17. 它的元素特点是: 无序(存取顺序不一致), 唯一.
  18. 它的常用子类主要有:
  19. HashSet: 底层数据结构采用的是哈希表(就是数组 + 链表)实现的, 增删,查询相对都快.
  20. TreeSet: //目前了解.
  21. */
  22. public class Demo01 {
  23. public static void main(String[] args) {
  24. //1. 创建集合对象.
  25. Set<String> hs = new HashSet<>();
  26. //2. 创建元素对象.
  27. //3. 把元素对象添加到集合中.
  28. hs.add("hello");
  29. hs.add("world");
  30. hs.add("java");
  31. hs.add("world");
  32.  
  33. //4. 遍历集合.
  34. //方式一: 普通迭代器.
  35. Iterator<String> it = hs.iterator();
  36. while (it.hasNext()) {
  37. String s = it.next();
  38. System.out.println(s);
  39. }
  40. System.out.println("----------------------");
  41.  
  42. //方式二: 增强for
  43. for (String h : hs) {
  44. System.out.println(h);
  45. }
  46. }
  47. }
  1. package com.it.demo05_set;
  2.  
  3. import com.it.pojo.Student;
  4.  
  5. /*
  6. 案例: 演示哈希值入门.
  7.  
  8. 需求:
  9. 1.定义学生类, 属性为姓名和年龄.
  10. 2.在测试类的main方法中, 创建两个学生对象, 分别获取它们的哈希值, 并打印.
  11. 3.测试: 重写Object#hashCode()方法, 实现不同对象的哈希值也是相同的.
  12. 4.测试: 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
  13.  
  14. 涉及到的Object类中的成员方法:
  15. public int hashCode();
  16. 获取指定对象的哈希值, 所谓的哈希值就是根据对象的属性等算出来的一个int类型的值.
  17. 因为在实际开发中, 我们认为如果对象的各个属性值都相同, 那么它们就是同一个对象,
  18. 所以子类一般都会重写Object#hashCode()方法, 采用根据属性值等来计算哈希值.
  19.  
  20. 结论(记忆):
  21. 1. 同一对象不管调用多少次 hashCode()方法, 获取到的哈希值都是相同的.
  22. 2. 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
  23. */
  24. public class Demo02 {
  25. public static void main(String[] args) {
  26. //1. 创建两个学生对象.
  27. Student s1 = new Student("刘亦菲", 33);
  28. Student s2 = new Student("高圆圆", 35);
  29. //2. 打印它们的哈希值.
  30. //测试: 同一对象不管调用多少次 hashCode()方法, 获取到的哈希值都是相同的.
  31. System.out.println(s1.hashCode());
  32. System.out.println(s1.hashCode());
  33. System.out.println(s2.hashCode());
  34. System.out.println("------------------");
  35.  
  36. //3. 测试: 同一对象哈希值肯定相同, 不同对象哈希值一般不同.
  37. //重地和通话, 儿女和农丰
  38. System.out.println("重地".hashCode());
  39. System.out.println("通话".hashCode());
  40. System.out.println("------------------");
  41. System.out.println("儿女".hashCode());
  42. System.out.println("农丰".hashCode());
  43. }
  44. }
  1. package com.it.demo05_set;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. /*
  8. 案例: 演示HashSet集合入门.
  9.  
  10. 需求:
  11. 1.创建Set集合对象, 存储字符串数据: "hello", "world", "java", "world"
  12. 2.通过两种方式, 遍历Set集合.
  13.  
  14. Set集合简介:
  15. 概述:
  16. 它属于Collection集合(单列集合)的子体系, 它是一个接口, 所以不能直接new,
  17. 它的元素特点是: 无序(存取顺序不一致), 唯一, 元素无索引.
  18. 它的常用子类主要有:
  19. HashSet: 底层数据结构采用的是哈希表(就是数组 + 链表)实现的, 增删,查询相对都快.
  20. TreeSet: //目前了解.
  21. */
  22. public class Demo03 {
  23. public static void main(String[] args) {
  24. //1. 创建集合对象.
  25. HashSet<String> hs = new HashSet<>();
  26. //2. 创建元素对象.
  27. //3. 把元素对象添加到集合中.
  28. hs.add("hello");
  29. hs.add("world");
  30. hs.add("java");
  31. hs.add("world");
  32. hs.add(null);
  33. hs.add(null);
  34.  
  35. //4. 遍历集合.
  36. //方式一: 普通迭代器.
  37. Iterator<String> it = hs.iterator();
  38. while (it.hasNext()) {
  39. String s = it.next();
  40. System.out.println(s);
  41. }
  42. System.out.println("----------------------");
  43.  
  44. //方式二: 增强for
  45. for (String h : hs) {
  46. System.out.println(h);
  47. }
  48. }
  49. }
  1. package com.it.demo05_set;
  2.  
  3. import java.util.HashSet;
  4. import java.util.LinkedHashSet;
  5. import java.util.Set;
  6.  
  7. /*
  8. 案例: 演示LinkedHashSet集合的使用.
  9.  
  10. LinkedHashSet简介:
  11. 它是HashSet集合的子类, 底层是采用 链表 + 哈希表的形式实现的, 元素特点是: 有序, 唯一.
  12.  
  13. 需求:
  14. 1.创建LinkedHashSet集合对象, 存储字符串"hello", "world", "java", "world"
  15. 2.遍历集合, 并将结果打印到控制台上.
  16. */
  17. public class Demo04 {
  18. public static void main(String[] args) {
  19. //1. 创建集合对象.
  20. LinkedHashSet<String> lhs = new LinkedHashSet<>();
  21. //2. 创建元素对象.
  22. //3. 把元素对象添加到集合中.
  23. lhs.add("hello");
  24. lhs.add("world");
  25. lhs.add("java");
  26. lhs.add("world");
  27.  
  28. //4. 遍历集合.
  29. for (String s : lhs) {
  30. System.out.println(s);
  31. }
  32.  
  33. }
  34. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. /*
  8. * Set接口,特点不重复元素,没索引
  9. *
  10. * Set接口的实现类,HashSet (哈希表)
  11. * 特点: 无序集合,存储和取出的顺序不同,没有索引,不存储重复元素
  12. * 代码的编写上,和ArrayList完全一致
  13. */
  14. public class HashSetDemo {
  15. public static void main(String[] args) {
  16. Set<String> set = new HashSet<String>();
  17. set.add("cn");
  18. set.add("heima");
  19. set.add("java");
  20. set.add("java");
  21. set.add("itcast");
  22.  
  23. Iterator<String> it = set.iterator();
  24. while(it.hasNext()){
  25. System.out.println(it.next());
  26. }
  27. System.out.println("==============");
  28.  
  29. for(String s : set){
  30. System.out.println(s);
  31. }
  32. }
  33. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.HashSet;
  4.  
  5. import cn.it.demo3.Person;
  6.  
  7. /*
  8. * HashSet集合的自身特点:
  9. * 底层数据结构,哈希表
  10. * 存储,取出都比较快
  11. * 线程不安全,运行速度快
  12. */
  13. public class HashSetDemo1 {
  14. public static void main(String[] args) {
  15. /*HashSet<String> set = new HashSet<String>();
  16. set.add(new String("abc"));
  17. set.add(new String("abc"));
  18. set.add(new String("bbc"));
  19. set.add(new String("bbc"));
  20. System.out.println(set);*/
  21.  
  22. //将Person对象中的姓名,年龄,相同数据,看作同一个对象
  23. //判断对象是否重复,依赖对象自己的方法 hashCode,equals
  24. HashSet<Person> setPerson = new HashSet<Person>();
  25. setPerson.add(new Person("a",11));
  26. setPerson.add(new Person("b",10));
  27. setPerson.add(new Person("b",10));
  28. setPerson.add(new Person("c",25));
  29. setPerson.add(new Person("d",19));
  30. setPerson.add(new Person("e",17));
  31. System.out.println(setPerson);
  32. }
  33. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.LinkedHashSet;
  4.  
  5. /*
  6. * LinkedHashSet 基于链表的哈希表实现
  7. * 继承自HashSet
  8. *
  9. * LinkedHashSet 自身特性,具有顺序,存储和取出的顺序相同的
  10. * 线程不安全的集合,运行速度块
  11. */
  12. public class LinkedHashSetDemo {
  13.  
  14. public static void main(String[] args) {
  15. LinkedHashSet<Integer> link = new LinkedHashSet<Integer>();
  16. link.add(123);
  17. link.add(44);
  18. link.add(33);
  19. link.add(33);
  20. link.add(66);
  21. link.add(11);
  22. System.out.println(link);
  23. }
  24. }
  1. package cn.it.demo3;
  2. /*
  3. * 对象的哈希值,普通的十进制整数
  4. * 父类Object,方法 public int hashCode() 计算结果int整数
  5. */
  6. public class HashDemo {
  7. public static void main(String[] args) {
  8. Person p = new Person();
  9. int i = p.hashCode();
  10. System.out.println(i);
  11.  
  12. String s1 = new String("abc");
  13. String s2 = new String("abc");
  14. System.out.println(s1.hashCode());
  15. System.out.println(s2.hashCode());
  16.  
  17. /*System.out.println("重地".hashCode());
  18. System.out.println("通话".hashCode());*/
  19. }
  20. }
  1. package cn.it.demo3;
  2. /*
  3. * 两个对象 Person p1 p2
  4. * 问题: 如果两个对象的哈希值相同 p1.hashCode()==p2.hashCode()
  5. * 两个对象的equals一定返回true吗 p1.equals(p2) 一定是true吗
  6. * 正确答案:不一定
  7. *
  8. * 如果两个对象的equals方法返回true,p1.equals(p2)==true
  9. * 两个对象的哈希值一定相同吗
  10. * 正确答案: 一定
  11. */
  12. public class HashDemo1 {
  13.  
  14. }
  1. package cn.it.demo3;
  2.  
  3. public class Person {
  4. private String name;
  5. private int age;
  6.  
  7. /*
  8. * 没有做重写父类,每次运行结果都是不同整数
  9. * 如果子类重写父类的方法,哈希值,自定义的
  10. * 存储到HashSet集合的依据
  11. */
  12. public int hashCode(){
  13. return name.hashCode()+age*55;
  14. }
  15. //方法equals重写父类,保证和父类相同
  16. //public boolean equals(Object obj){}
  17. public boolean equals(Object obj){
  18. if(this == obj)
  19. return true;
  20. if(obj == null)
  21. return false;
  22. if(obj instanceof Person){
  23. Person p = (Person)obj;
  24. return name.equals(p.name) && age==p.age;
  25. }
  26. return false;
  27. }
  28.  
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public int getAge() {
  36. return age;
  37. }
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41. public Person(String name, int age) {
  42. super();
  43. this.name = name;
  44. this.age = age;
  45. }
  46. public Person(){}
  47.  
  48. public String toString(){
  49. return name+".."+age;
  50. }
  51.  
  52. }
  1. package com.it.demo06_change_variable;
  2.  
  3. import javax.sound.midi.Soundbank;
  4.  
  5. /*
  6. 案例: 可变参数详解.
  7.  
  8. 需求:
  9. 1.定义getSum()方法, 用来获取n个整数的和(n可能是任意的一个数字).
  10. 2.在main方法中, 调用getSum()方法.
  11.  
  12. 可变参数简介:
  13. 概述:
  14. 它是JDK1.5的特性, 也叫: 参数的个数可变, 一般是用作方法的形参列表的.
  15. 格式:
  16. 数据类型... 变量名 //就是在数据类型的后边加上 三个点
  17. 细节:
  18. 1. 可变参数的本质就是一个数组.
  19. 2. 一个方法形参列表有且只能有一个可变参数, 且可变参数必须放在参数列表的最后.
  20. */
  21. public class Demo01 {
  22. public static void main(String[] args) {
  23. //2.在main方法中, 调用getSum()方法.
  24. int[] arr = {1, 2, 3, 4, 5};
  25. System.out.println(getSum(arr)); //不报错, 说明可变参数的本质就是一个数组
  26. System.out.println("----------------------");
  27.  
  28. System.out.println(getSum()); //可变参数意味着参数个数可变, 里边可以传入的参数个数至少0个, 至多无所谓
  29. System.out.println(getSum(1, 2, 3)); //可变参数意味着参数个数可变, 里边可以传入的参数个数至少0个, 至多无所谓
  30. System.out.println("----------------------");
  31.  
  32. }
  33.  
  34. //1.定义getSum()方法, 用来获取n个整数的和(n可能是任意的一个数字).
  35. //方式二: 可变参数版
  36.  
  37. // 一个方法形参列表有且只能有一个可变参数, 且可变参数必须放在参数列表的最后.
  38. //public static int getSum(int... arr, int... arr) { //报错, 一个方法形参列表有且只能有一个可变参数
  39. //public static int getSum(int... arr, int a) { //报错, 可变参数必须放在参数列表的最后.
  40.  
  41. public static int getSum(int... arr) { //可变参数的本质就是一个数组
  42. int sum = 0;
  43. for (int i = 0; i < arr.length; i++) {
  44. sum += arr[i];
  45. }
  46. return sum;
  47. }
  48.  
  49. //方式一: 普通版
  50. /* public static int getSum(int a, int b) {
  51. return a + b;
  52. }
  53.  
  54. public static int getSum(int a, int b, int c) {
  55. return a + b + c;
  56. }
  57.  
  58. public static int getSum(int a, int b, int c, int d) {
  59. return a + b + c + d;
  60. }*/
  61. }
  1. package com.it.demo07_map;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /*
  7. 案例: 演示Map集合入门.
  8.  
  9. 需求:
  10. 1.定义Map集合, 键是学号, 值是学生的名字. (键值都是字符串类型).
  11. 2.往Map集合中添加3对元素.
  12. 3.打印Map集合对象.
  13.  
  14. Map集合简介:
  15. 概述:
  16. 它是双列集合的顶层接口, 存储的是 键值对对象元素(由键和值两部分组成), 其中键具有唯一性, 值可以重复.
  17. 双列集合的数据结构只针对于 键 有效.
  18. 它的常用子类有: HashMap 和 TreeMap.
  19. 成员方法:
  20. public V put(K key, V value); 添加元素.
  21. */
  22. public class Demo01 {
  23. public static void main(String[] args) {
  24. //需求: 定义Map集合, 键是学号, 值是学生的名字. (键值都是字符串类型).
  25. //1. 创建集合.
  26. Map<String ,String> hm = new HashMap<>();
  27. //2. 创建元素.
  28. //3. 把元素添加到集合中.
  29. hm.put("heima001", "刘亦菲");
  30. hm.put("heima002", "赵丽颖");
  31. hm.put("heima003", "高圆圆");
  32.  
  33. //4. 遍历集合, 因为Map集合的遍历方式和Collection不太一样, 我们稍后详解.
  34. //这里我们先直接输出Map集合.
  35. System.out.println(hm);
  36. }
  37. }
  1. package com.it.demo07_map;
  2.  
  3. import java.util.HashMap;
  4.  
  5. /*
  6. 案例: 演示Map集合的基本方法.
  7.  
  8. 涉及到的Map集合中的成员方法:
  9. V put(K key,V value) 添加元素, 键不存在, 表示第一次添加, 返回null, 键存在, 表示重复添加, 会用新值覆盖旧值, 并返回旧值.
  10. V remove(Object key) 根据键删除键值对元素, 便返回删除之前的值, 如果键不存在, 返回null
  11. void clear() 移除所有的键值对元素
  12. boolean containsKey(Object key) 判断集合是否包含指定的键
  13. boolean containsValue(Object value) 判断集合是否包含指定的值
  14. boolean isEmpty() 判断集合是否为空
  15. int size() 集合的长度,也就是集合中键值对的个数
  16.  
  17. 需求:
  18. 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
  19. 2.分别测试上述的7个方法.
  20. */
  21. public class Demo02 {
  22. public static void main(String[] args) {
  23. //需求: 定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
  24. //1. 创建集合对象.
  25. HashMap<String, String> hm = new HashMap<>();
  26. //2. 创建元素对象.
  27. //3. 把元素对象添加到集合中.
  28. hm.put("乔峰", "阿朱");
  29. hm.put("虚竹", "梦姑");
  30. hm.put("段誉", "王语嫣");
  31. hm.put("慕容复", "王语嫣");
  32.  
  33. //测试: put()方法
  34. //System.out.println(hm.put("乔峰", "阿朱")); //null
  35. //System.out.println(hm.put("乔峰", "阿紫")); //阿朱
  36.  
  37. //测试: V remove(Object key) 根据键删除键值对元素
  38. //System.out.println(hm.remove("乔峰")); //阿朱
  39. //System.out.println(hm.remove("杨过")); //null
  40.  
  41. //测试: void clear() 移除所有的键值对元素
  42. //hm.clear();
  43.  
  44. //测试: boolean containsKey(Object key) 判断集合是否包含指定的键
  45. //System.out.println(hm.containsKey("杨过")); //false
  46. //System.out.println(hm.containsKey("段誉")); //true
  47.  
  48. //测试: boolean containsValue(Object value) 判断集合是否包含指定的值, 自己写.
  49.  
  50. //测试: boolean isEmpty() 判断集合是否为空, 当且仅当集合的长度为0时, 返回true, 其他返回false
  51. //System.out.println(hm.isEmpty());
  52.  
  53. //测试: int size() 集合的长度,也就是集合中键值对的个数
  54. System.out.println(hm.size()); //3
  55.  
  56. //4. 打印集合.
  57. System.out.println(hm);
  58. }
  59. }
  1. package com.it.demo07_map;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.Set;
  6.  
  7. /*
  8. 案例: 演示Map集合的获取功能.
  9.  
  10. 涉及到的Map集合中的成员方法:
  11. V get(Object key) 根据键获取值
  12. Set keySet() 获取所有键的集合
  13. Collection values() 获取所有值的集合
  14. Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的集合
  15.  
  16. 需求:
  17. 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
  18. 2.先通过代码测试上述的3个方法. 即: get(), keySet(), values()
  19. */
  20. public class Demo03 {
  21. public static void main(String[] args) {
  22. //1. 创建集合对象.
  23. HashMap<String, String> hm = new HashMap<>();
  24. //2. 创建元素对象.
  25. //3. 把元素对象添加到集合中.
  26. hm.put("乔峰", "阿朱");
  27. hm.put("虚竹", "梦姑");
  28. hm.put("段誉", "王语嫣");
  29. hm.put("慕容复", "王语嫣");
  30.  
  31. //测试: V get(Object key) 根据键获取值
  32. System.out.println(hm.get("乔峰"));
  33. System.out.println(hm.get("虚竹"));
  34. System.out.println(hm.get("段誉"));
  35. System.out.println(hm.get("慕容复"));
  36. System.out.println("-----------------");
  37.  
  38. //测试: Set keySet() 获取所有键的集合
  39. Set<String> keys = hm.keySet();
  40. //通过增强for遍历 Set集合
  41. for (String key : keys) {
  42. System.out.println(key);
  43. }
  44. System.out.println("-----------------");
  45.  
  46. //测试: Collection values() 获取所有值的集合
  47. Collection<String> values = hm.values();
  48. for (String value : values) {
  49. System.out.println(value);
  50. }
  51.  
  52. }
  53. }
  1. package com.it.demo07_map;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. /*
  8. 案例: 演示Map集合的遍历方式一, 根据键获取其对应的值.
  9.  
  10. 需求:
  11. 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
  12. 2.往集合中添加3对键值对元素.
  13. 3.遍历Map集合.
  14.  
  15. 思路:
  16. 大白话版: 根据丈夫找妻子.
  17. 1. 获取所有的丈夫的集合.
  18. 2. 遍历, 获取到每一个丈夫.
  19. 3. 根据丈夫找到其对应的妻子.
  20. 专业版:
  21. 1. 获取所有键的集合. Map#keySet()
  22. 2. 遍历, 获取到每一个键. 迭代器, 增强for.
  23. 3. 根据键获取其对应的值. Map#get();
  24. */
  25. public class Demo04 {
  26. public static void main(String[] args) {
  27. //1. 创建集合对象.
  28. HashMap<String, String> hm = new HashMap<>();
  29. //2. 创建元素对象.
  30. //3. 把元素对象添加到集合中.
  31. hm.put("乔峰", "阿朱");
  32. hm.put("虚竹", "梦姑");
  33. hm.put("段誉", "王语嫣");
  34.  
  35. //4. 遍历Map集合.
  36. //4.1 获取所有键的集合. Map#keySet()
  37. Set<String> keys = hm.keySet();
  38. //4.2 遍历, 获取到每一个键. 迭代器, 增强for.
  39. //方式一: 增强for
  40. for (String key : keys) {
  41. //4.3 根据键获取其对应的值. Map#get();
  42. String value = hm.get(key);
  43. System.out.println(key + "..." + value);
  44. }
  45. System.out.println("--------------------------");
  46.  
  47. //方式二: 迭代器, 目前了解即可.
  48. Iterator<String> it = keys.iterator();
  49. while (it.hasNext()) {
  50. String key = it.next();
  51. String value = hm.get(key);
  52. System.out.println(key + "..." + value);
  53. }
  54.  
  55. }
  56. }
  1. package com.it.demo07_map;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. /*
  9. 案例: 演示Map集合的遍历方式二, 根据 键值对对象 获取其对应的 键和值.
  10.  
  11. 需求:
  12. 1.定义Map集合, 键是丈夫, 值是妻子. (键值都是字符串类型).
  13. 2.往集合中添加3对键值对元素.
  14. 3.遍历Map集合.
  15.  
  16. 思路:
  17. 大白话版: 根据 结婚证 找 丈夫和妻子.
  18. 1. 获取所有的 结婚证 的集合.
  19. 2. 遍历, 获取到每一个 结婚证.
  20. 3. 根据 结婚证 找到其对应的 丈夫和妻子.
  21. 专业版:
  22. 1. 获取所有 键值对对象 的集合. Map#entrySet() 键值对对象的数据类型: Map.Entry<K, V>
  23. 2. 遍历, 获取到每一个 键值对对象. 迭代器, 增强for.
  24. 3. 根据 键值对对象 获取其对应的 键和值. Map.Entry接口中的方法: getKey(), getValue();
  25. */
  26. public class Demo05 {
  27. public static void main(String[] args) {
  28. //1. 创建集合对象.
  29. HashMap<String, String> hm = new HashMap<>();
  30. //2. 创建元素对象.
  31. //3. 把元素对象添加到集合中.
  32. hm.put("乔峰", "阿朱");
  33. hm.put("虚竹", "梦姑");
  34. hm.put("段誉", "王语嫣");
  35.  
  36. //4. 遍历Map集合.
  37. //4.1 获取所有 键值对对象 的集合.
  38. //Set<String> keys = hm.keySet(); //String: 键的数据类型
  39. Set<Map.Entry<String, String>> entrys = hm.entrySet(); //Map.Entry<String, String>: 键值对对象的数据类型
  40. //4.2 遍历, 获取到每一个 键值对对象.
  41. //方式一: 增强for
  42. for (Map.Entry<String, String> entry : entrys) {
  43. //entry: 就表示Map集合中的每一组元素, 例如: "虚竹", "梦姑"
  44. //4.3 根据 键值对对象 获取其对应的 键和值.
  45. System.out.println(entry.getKey() + "..." + entry.getValue());
  46. }
  47.  
  48. System.out.println("--------------------------");
  49.  
  50. //方式二: 迭代器, 目前了解即可.
  51. Iterator<Map.Entry<String, String>> it = entrys.iterator();
  52. while (it.hasNext()) {
  53. //entry: 就表示Map集合中的每一组元素, 例如: "虚竹", "梦姑"
  54. Map.Entry<String, String> entry = it.next();
  55. System.out.println(entry.getKey() + "..." + entry.getValue());
  56. }
  57.  
  58. }
  59. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /*
  7. * Map接口中的常用方法
  8. * 使用Map接口的实现类 HashMap
  9. */
  10. public class MapDemo {
  11. public static void main(String[] args) {
  12. function_2();
  13. }
  14. /*
  15. * 移除集合中的键值对,返回被移除之前的值
  16. * V remove(K)
  17. */
  18. public static void function_2(){
  19. Map<Integer,String> map = new HashMap<Integer, String>();
  20. map.put(1, "a");
  21. map.put(2, "b");
  22. map.put(3, "c");
  23. System.out.println(map);
  24.  
  25. String value = map.remove(33);
  26. System.out.println(value);
  27. System.out.println(map);
  28. }
  29.  
  30. /*
  31. * 通过键对象,获取值对象
  32. * V get(K)
  33. * 如果集合中没有这个键,返回null
  34. */
  35. public static void function_1(){
  36. //创建集合对象,作为键的对象整数,值的对象存储字符串
  37. Map<Integer,String> map = new HashMap<Integer, String>();
  38. map.put(1, "a");
  39. map.put(2, "b");
  40. map.put(3, "c");
  41. System.out.println(map);
  42.  
  43. String value = map.get(4);
  44. System.out.println(value);
  45. }
  46.  
  47. /*
  48. * 将键值对存储到集合中
  49. * V put(K,V) K 作为键的对象, V作为值的对象
  50. * 存储的是重复的键,将原有的值,覆盖
  51. * 返回值一般情况下返回null,
  52. * 存储重复键的时候,返回被覆盖之前的值
  53. */
  54. public static void function(){
  55. //创建集合对象,HashMap,存储对象,键是字符串,值是整数
  56. Map<String, Integer> map = new HashMap<String, Integer>();
  57. map.put("a", 1);
  58.  
  59. map.put("b", 2);
  60.  
  61. map.put("c", 3);
  62.  
  63. System.out.println(map);
  64. }
  65. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. /*
  9. * Map集合的遍历
  10. * 利用键获取值
  11. * Map接口中定义方法keySet
  12. * 所有的键,存储到Set集合
  13. */
  14. public class MapDemo1 {
  15. public static void main(String[] args) {
  16. /*
  17. * 1. 调用map集合的方法keySet,所有的键存储到Set集合中
  18. * 2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
  19. * 3. 调用map集合方法get,通过键获取到值
  20. */
  21. Map<String,Integer> map = new HashMap<String,Integer>();
  22. map.put("a", 11);
  23. map.put("b", 12);
  24. map.put("c", 13);
  25. map.put("d", 14);
  26.  
  27. //1. 调用map集合的方法keySet,所有的键存储到Set集合中
  28. Set<String> set = map.keySet();
  29. //2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
  30. Iterator<String> it = set.iterator();
  31. while(it.hasNext()){
  32. //it.next返回是Set集合元素,也就是Map中的键
  33. //3. 调用map集合方法get,通过键获取到值
  34. String key = it.next();
  35. Integer value = map.get(key);
  36. System.out.println(key+"...."+value);
  37. }
  38. System.out.println("=======================");
  39. for(String key : map.keySet()){
  40. Integer value = map.get(key);
  41. System.out.println(key+"...."+value);
  42. }
  43. }
  44. }
  1. package cn.it.demo1;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. /*
  9. * Map集合获取方式
  10. * entrySet方法,键值对映射关系(结婚证)获取
  11. * 实现步骤:
  12. * 1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
  13. * Set<Entry <K,V> >
  14. * 2. 迭代Set集合
  15. * 3. 获取出的Set集合的元素,是映射关系对象
  16. * 4. 通过映射关系对象方法 getKet, getValue获取键值对
  17. *
  18. * 创建内部类对象 外部类.内部类 = new
  19. */
  20. public class MapDemo2 {
  21. public static void main(String[] args) {
  22. Map<Integer,String> map = new HashMap<Integer, String>();
  23. map.put(1, "abc");
  24. map.put(2, "bcd");
  25. map.put(3, "cde");
  26. //1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
  27. Set<Map.Entry <Integer,String> > set = map.entrySet();
  28. //2. 迭代Set集合
  29. Iterator<Map.Entry <Integer,String> > it = set.iterator();
  30. while(it.hasNext()){
  31. // 3. 获取出的Set集合的元素,是映射关系对象
  32. // it.next 获取的是什么对象,也是Map.Entry对象
  33. Map.Entry<Integer, String> entry = it.next();
  34. //4. 通过映射关系对象方法 getKet, getValue获取键值对
  35. Integer key = entry.getKey();
  36. String value = entry.getValue();
  37. System.out.println(key+"...."+value);
  38. }
  39.  
  40. System.out.println("=========================");
  41. for(Map.Entry<Integer, String> entry : map.entrySet()){
  42. System.out.println(entry.getKey()+"..."+entry.getValue());
  43. }
  44. }
  45. }
  1. package cn.it.demo2;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /*
  7. * 使用HashMap集合,存储自定义的对象
  8. * 自定义对象,作为键,出现,作为值出现
  9. */
  10. public class HashMapDemo {
  11. public static void main(String[] args) {
  12. function_1();
  13. }
  14. /*
  15. * HashMap 存储自定义对象Person,作为键出现
  16. * 键的对象,是Person类型,值是字符串
  17. * 保证键的唯一性,存储到键的对象,重写hashCode equals
  18. */
  19. public static void function_1(){
  20. HashMap<Person, String> map = new HashMap<Person, String>();
  21. map.put(new Person("a",20), "里约热内卢");
  22. map.put(new Person("b",18), "索马里");
  23. map.put(new Person("b",18), "索马里");
  24. map.put(new Person("c",19), "百慕大");
  25. for(Person key : map.keySet()){
  26. String value = map.get(key);
  27. System.out.println(key+"..."+value);
  28. }
  29. System.out.println("===================");
  30. for(Map.Entry<Person, String> entry : map.entrySet()){
  31. System.out.println(entry.getKey()+"..."+entry.getValue());
  32. }
  33. }
  34.  
  35. /*
  36. * HashMap 存储自定义的对象Person,作为值出现
  37. * 键的对象,是字符串,可以保证唯一性
  38. */
  39. public static void function(){
  40. HashMap<String, Person> map = new HashMap<String, Person>();
  41. map.put("beijing", new Person("a",20));
  42. map.put("tianjin", new Person("b",18));
  43. map.put("shanghai", new Person("c",19));
  44. for(String key : map.keySet()){
  45. Person value = map.get(key);
  46. System.out.println(key+"..."+value);
  47. }
  48. System.out.println("=================");
  49. for(Map.Entry<String, Person> entry : map.entrySet()){
  50. String key = entry.getKey();
  51. Person value = entry.getValue();
  52. System.out.println(key+"..."+value);
  53. }
  54. }
  55. }
  1. package cn.it.demo2;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Map;
  5.  
  6. /*
  7. * Map接口实现类 Hashtable
  8. * 底层数据结果哈希表,特点和HashMap是一样的
  9. * Hashtable 线程安全集合,运行速度慢
  10. * HashMap 线程不安全的集合,运行速度快
  11. *
  12. * Hashtable命运和Vector是一样的,从JDK1.2开始,被更先进的HashMap取代
  13. *
  14. * HashMap 允许存储null值,null键
  15. * Hashtable 不允许存储null值,null键
  16. *
  17. * Hashtable他的孩子,子类 Properties 依然活跃在开发舞台
  18. */
  19. public class HashtableDemo {
  20. public static void main(String[] args) {
  21. Map<String,String> map = new Hashtable<String,String>();
  22. map.put(null, null);
  23. System.out.println(map);
  24. }
  25. }
  1. package cn.it.demo2;
  2.  
  3. import java.util.LinkedHashMap;
  4.  
  5. /*
  6. * LinkedHashMap继承HashMap
  7. * 保证迭代的顺序
  8. */
  9. public class LinkedHashMapDemo {
  10. public static void main(String[] args) {
  11. LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
  12. link.put("1", "a");
  13. link.put("13", "a");
  14. link.put("15", "a");
  15. link.put("17", "a");
  16. System.out.println(link);
  17. }
  18. }
  1. package cn.it.demo2;
  2.  
  3. public class Person {
  4. private String name;
  5. private int age;
  6.  
  7. @Override
  8. public int hashCode() {
  9. final int prime = 31;
  10. int result = 1;
  11. result = prime * result + age;
  12. result = prime * result + ((name == null) ? 0 : name.hashCode());
  13. return result;
  14. }
  15. @Override
  16. public boolean equals(Object obj) {
  17. if (this == obj)
  18. return true;
  19. if (obj == null)
  20. return false;
  21. if (getClass() != obj.getClass())
  22. return false;
  23. Person other = (Person) obj;
  24. if (age != other.age)
  25. return false;
  26. if (name == null) {
  27. if (other.name != null)
  28. return false;
  29. } else if (!name.equals(other.name))
  30. return false;
  31. return true;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public void setName(String name) {
  37. this.name = name;
  38. }
  39. public int getAge() {
  40. return age;
  41. }
  42. public void setAge(int age) {
  43. this.age = age;
  44. }
  45. public Person(String name, int age) {
  46. super();
  47. this.name = name;
  48. this.age = age;
  49. }
  50. public Person() {
  51. super();
  52.  
  53. }
  54. @Override
  55. public String toString() {
  56. return "Person " + name +"...."+ age ;
  57. }
  58.  
  59. }
  1. package cn.it.demo3;
  2. /*
  3. * JDK1.5新特性,静态导入
  4. * 减少开发的代码量
  5. * 标准的写法,导入包的时候才能使用
  6. *
  7. * import static java.lang.System.out;最末尾,必须是一个静态成员
  8. */
  9. import static java.lang.System.out;
  10. import static java.util.Arrays.sort;
  11.  
  12. public class StaticImportDemo {
  13. public static void main(String[] args) {
  14. out.println("hello");
  15.  
  16. int[] arr = {1,4,2};
  17. sort(arr);
  18. }
  19. }
  1. package cn.it.demo3;
  2. /*
  3. * JDK1.5新的特性,方法的可变参数
  4. * 前提: 方法参数数据类型确定,参数的个数任意
  5. * 可变参数语法: 数据类型...变量名
  6. * 可变参数,本质就是一个数组
  7. */
  8. public class VarArgumentsDemo {
  9. public static void main(String[] args) {
  10. //调用一个带有可变参数的方法,传递参数,可以任意
  11. // getSum();
  12. int sum = getSum(5,34,3,56,7,8,0);
  13. System.out.println(sum);
  14.  
  15. function(1,2,3);
  16. }
  17. /*
  18. * 可变参数的注意事项
  19. * 1. 一个方法中,可变参数只能有一个
  20. * 2. 可变参数,必须写在参数列表的最后一位
  21. */
  22. public static void function(Object...o){
  23.  
  24. }
  25.  
  26. /*
  27. * 定义方法,计算10个整数和
  28. * 方法的可变参数实现
  29. */
  30. public static int getSum(int...a){
  31. int sum = 0 ;
  32. for(int i : a){
  33. sum = sum + i;
  34. }
  35. return sum;
  36. }
  37.  
  38. /*
  39. * 定义方法,计算3个整数和
  40. */
  41. /*public static int getSum(int a,int b ,int c){
  42. return a+b+c;
  43. }*/
  44.  
  45. /*
  46. * 定义方法,计算2个整数和
  47. */
  48. /*public static int getSum(int a,int b){
  49. return a+b;
  50. }*/
  51. }
  1. package cn.it.demo4;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. /*
  8. * 集合操作的工具类
  9. * Collections
  10. */
  11. public class CollectionsDemo {
  12. public static void main(String[] args) {
  13. function_2();
  14. }
  15. /*
  16. * Collections.shuffle方法
  17. * 对List集合中的元素,进行随机排列
  18. */
  19. public static void function_2(){
  20. List<Integer> list = new ArrayList<Integer>();
  21. list.add(1);
  22. list.add(5);
  23. list.add(9);
  24. list.add(11);
  25. list.add(8);
  26. list.add(10);
  27. list.add(15);
  28. list.add(20);
  29. System.out.println(list);
  30.  
  31. //调用工具类方法shuffle对集合随机排列
  32. Collections.shuffle(list);
  33. System.out.println(list);
  34. }
  35.  
  36. /*
  37. * Collections.binarySearch静态方法
  38. * 对List集合进行二分搜索,方法参数,传递List集合,传递被查找的元素
  39. */
  40. public static void function_1(){
  41. List<Integer> list = new ArrayList<Integer>();
  42. list.add(1);
  43. list.add(5);
  44. list.add(8);
  45. list.add(10);
  46. list.add(15);
  47. list.add(20);
  48. //调用工具类静态方法binarySearch
  49. int index = Collections.binarySearch(list, 16);
  50. System.out.println(index);
  51. }
  52.  
  53. /*
  54. * Collections.sort静态方法
  55. * 对于List集合,进行升序排列
  56. */
  57. public static void function(){
  58. //创建List集合
  59. List<String> list = new ArrayList<String>();
  60. list.add("ewrew");
  61. list.add("qwesd");
  62. list.add("Qwesd");
  63. list.add("bv");
  64. list.add("wer");
  65. System.out.println(list);
  66. //调用集合工具类的方法sort
  67. Collections.sort(list);
  68. System.out.println(list);
  69. }
  70. }
  1. package cn.it.demo5;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. /*
  9. * Map集合的嵌套,Map中存储的还是Map集合
  10. * 要求:
  11. * 传智播客
  12. * Java基础班
  13. * 001 张三
  14. * 002 李四
  15. *
  16. * Java就业班
  17. * 001 王五
  18. * 002 赵六
  19. * 对以上数据进行对象的存储
  20. * 001 张三 键值对
  21. * Java基础班: 存储学号和姓名的键值对
  22. * Java就业班:
  23. * 传智播客: 存储的是班级
  24. *
  25. * 基础班Map <学号,姓名>
  26. * 传智播客Map <班级名字, 基础班Map>
  27. */
  28. public class MapMapDemo {
  29. public static void main(String[] args) {
  30. //定义基础班集合
  31. HashMap<String, String> javase = new HashMap<String, String>();
  32. //定义就业班集合
  33. HashMap<String, String> javaee = new HashMap<String, String>();
  34. //向班级集合中,存储学生信息
  35. javase.put("001", "张三");
  36. javase.put("002", "李四");
  37.  
  38. javaee.put("001", "王五");
  39. javaee.put("002", "赵六");
  40. //定义传智播客集合容器,键是班级名字,值是两个班级容器
  41. HashMap<String, HashMap<String,String>> czbk =
  42. new HashMap<String, HashMap<String,String>>();
  43. czbk.put("基础班", javase);
  44. czbk.put("就业班", javaee);
  45.  
  46. //keySet(czbk);
  47. entrySet(czbk);
  48. }
  49.  
  50. public static void entrySet(HashMap<String,HashMap<String,String>> czbk){
  51. //调用czbk集合方法entrySet方法,将czbk集合的键值对关系对象,存储到Set集合
  52. Set<Map.Entry<String, HashMap<String,String>>>
  53. classNameSet = czbk.entrySet();
  54. //迭代器迭代Set集合
  55. Iterator<Map.Entry<String, HashMap<String,String>>> classNameIt = classNameSet.iterator();
  56. while(classNameIt.hasNext()){
  57. //classNameIt.next方法,取出的是czbk集合的键值对关系对象
  58. Map.Entry<String, HashMap<String,String>> classNameEntry = classNameIt.next();
  59. //classNameEntry方法 getKey,getValue
  60. String classNameKey = classNameEntry.getKey();
  61. //获取值,值是一个Map集合
  62. HashMap<String,String> classMap = classNameEntry.getValue();
  63. //调用班级集合classMap方法entrySet,键值对关系对象存储Set集合
  64. Set<Map.Entry<String, String>> studentSet = classMap.entrySet();
  65. //迭代Set集合
  66. Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator();
  67. while(studentIt.hasNext()){
  68. //studentIt方法next获取出的是班级集合的键值对关系对象
  69. Map.Entry<String, String> studentEntry = studentIt.next();
  70. //studentEntry方法 getKey getValue
  71. String numKey = studentEntry.getKey();
  72. String nameValue = studentEntry.getValue();
  73. System.out.println(classNameKey+".."+numKey+".."+nameValue);
  74. }
  75. }
  76. System.out.println("==================================");
  77.  
  78. for (Map.Entry<String, HashMap<String, String>> me : czbk.entrySet()) {
  79. String classNameKey = me.getKey();
  80. HashMap<String, String> numNameMapValue = me.getValue();
  81. for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
  82. String numKey = nameMapEntry.getKey();
  83. String nameValue = nameMapEntry.getValue();
  84. System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
  85. }
  86. }
  87. }
  88.  
  89. public static void keySet(HashMap<String,HashMap<String,String>> czbk){
  90. //调用czbk集合方法keySet将键存储到Set集合
  91. Set<String> classNameSet = czbk.keySet();
  92. //迭代Set集合
  93. Iterator<String> classNameIt = classNameSet.iterator();
  94. while(classNameIt.hasNext()){
  95. //classNameIt.next获取出来的是Set集合元素,czbk集合的键
  96. String classNameKey = classNameIt.next();
  97. //czbk集合的方法get获取值,值是一个HashMap集合
  98. HashMap<String,String> classMap = czbk.get(classNameKey);
  99. //调用classMap集合方法keySet,键存储到Set集合
  100. Set<String> studentNum = classMap.keySet();
  101. Iterator<String> studentIt = studentNum.iterator();
  102.  
  103. while(studentIt.hasNext()){
  104. //studentIt.next获取出来的是classMap的键,学号
  105. String numKey = studentIt.next();
  106. //调用classMap集合中的get方法获取值
  107. String nameValue = classMap.get(numKey);
  108. System.out.println(classNameKey+".."+numKey+".."+nameValue);
  109. }
  110. }
  111.  
  112. System.out.println("==================================");
  113. for(String className: czbk.keySet()){
  114. HashMap<String, String> hashMap = czbk.get(className);
  115. for(String numKey : hashMap.keySet()){
  116. String nameValue = hashMap.get(numKey);
  117. System.out.println(className+".."+numKey+".."+nameValue);
  118. }
  119. }
  120. }
  121.  
  122. }
  1. package cn.it.demo6;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6.  
  7. /*
  8. * 实现模拟斗地主的功能
  9. * 1. 组合牌
  10. * 2. 洗牌
  11. * 3. 发牌
  12. * 4. 看牌
  13. */
  14. public class DouDiZhu {
  15. public static void main(String[] args) {
  16. //1. 组合牌
  17. //创建Map集合,键是编号,值是牌
  18. HashMap<Integer,String> pooker = new HashMap<Integer, String>();
  19. //创建List集合,存储编号
  20. ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
  21. //定义出13个点数的数组
  22. String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
  23. //定义4个花色数组
  24. String[] colors = {"","","",""};
  25. //定义整数变量,作为键出现
  26. int index = 2;
  27. //遍历数组,花色+点数的组合,存储到Map集合
  28. for(String number : numbers){
  29. for(String color : colors){
  30. pooker.put(index, color+number);
  31. pookerNumber.add(index);
  32. index++;
  33. }
  34. }
  35. //存储大王,和小王
  36. pooker.put(0, "大王");
  37. pookerNumber.add(0);
  38. pooker.put(1, "小王");
  39. pookerNumber.add(1);
  40.  
  41. //洗牌,将牌的编号打乱
  42. Collections.shuffle(pookerNumber);
  43.  
  44. //发牌功能,将牌编号,发给玩家集合,底牌集合
  45. ArrayList<Integer> player1 = new ArrayList<Integer>();
  46. ArrayList<Integer> player2 = new ArrayList<Integer>();
  47. ArrayList<Integer> player3 = new ArrayList<Integer>();
  48. ArrayList<Integer> bottom = new ArrayList<Integer>();
  49.  
  50. //发牌采用的是集合索引%3
  51. for(int i = 0 ; i < pookerNumber.size() ; i++){
  52. //先将底牌做好
  53. if(i < 3){
  54. //存到底牌去
  55. bottom.add( pookerNumber.get(i));
  56. //对索引%3判断
  57. }else if(i % 3 == 0){
  58. //索引上的编号,发给玩家1
  59. player1.add( pookerNumber.get(i) );
  60. }else if( i % 3 == 1){
  61. //索引上的编号,发给玩家2
  62. player2.add( pookerNumber.get(i) );
  63. }else if( i % 3 == 2){
  64. //索引上的编号,发给玩家3
  65. player3.add( pookerNumber.get(i) );
  66. }
  67. }
  68. //对玩家手中的编号排序
  69. Collections.sort(player1);
  70. Collections.sort(player2);
  71. Collections.sort(player3);
  72. //看牌,将玩家手中的编号,到Map集合中查找,根据键找值
  73. //定义方法实现
  74. look("刘德华",player1,pooker);
  75. look("张曼玉",player2,pooker);
  76. look("林青霞",player3,pooker);
  77. look("底牌",bottom,pooker);
  78. }
  79. public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
  80. //遍历ArrayList集合,获取元素,作为键,到集合Map中找值
  81. System.out.print(name+" ");
  82. for(Integer key : player){
  83. String value = pooker.get(key);
  84. System.out.print(value+" ");
  85. }
  86. System.out.println();
  87. }
  88. }

Java 复习整理day07的更多相关文章

  1. java 复习整理(一 java简介和基础语法)

    现在公司用的是封装太多东西的平台开发,觉着之前学的东西很多都忘了,所以想好好总结回顾一下.之前总是想学很多编程语言像python.s6.node.react,但现在越来越体会到编程语言只是一个开发的工 ...

  2. Java 复习整理day08

    package com.it.demo02_lambda; //接口, 表示动物. //public abstract class Animal { //报错, Lambda表达式只针对于接口有效 p ...

  3. java复习整理(六 异常处理)

    一.异常简介  在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出).Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性.        ...

  4. java 复习整理(五 类加载机制与对象初始化)

    类加载机制与对象初始化   一 . 类加载机制 类加载机制是指.class文件加载到jvm并形成Class对象的机制.之后应用可对Class对象进行实例化并调用.类加载机制可在运行时动态加载外部的类, ...

  5. java 复习整理(四 String类详解)

    String 类详解   StringBuilder与StringBuffer的功能基本相同,不同之处在于StringBuilder是非线程安全的,而StringBuffer是线程安全的,因此效率上S ...

  6. java 复习整理(三 修饰符)

    访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为default,在同一包内可见,不使用任何修饰符. 私有的,以pr ...

  7. java 复习整理(二 数据类型和几种变量)

    源文件声明规则 当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则. 一个源文件中只能有一个public类 一个源文件可以有多个非public类 源文件的名 ...

  8. Java 复习整理day10

    package com.it.demo01_quickstart; /* 案例: 讲解网络编程相关概念. 网络编程简介: 概述: 网络编程也叫: 套接字编程, Socket编程, 就是用来实现 网络互 ...

  9. Java 复习整理day09

    package com.it.demo01_thread; /* 案例: 多线程简介. 概述: 指的是进程有多条执行路径, 统称叫: 多线程. 进程: 指的是可执行程序, 文件(例如: .exe) 大 ...

随机推荐

  1. 项目中处理数据常用Excel公式

    ="'"&A1&"'," 需求:是大佬给了excel,里面是700多个单号,要我从生产的数据库中查询出每个单号对应的类型,这时需要查数据库,我决 ...

  2. 打算写一些Netty的文章了,先聊聊为什么要学习Netty

    微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 2021年了,终于开始系统性总结Netty相关的东西了. 这会是Netty系列的第一篇,我想先聊聊 "为 ...

  3. redis存json数据时选择string还是hash

    redis存json数据时选择string还是hash 我们在缓存json数据到redis时经常会面临是选择string类型还是选择hash类型去存储.接下来我从占用空间和IO两方面来分析这两种类型的 ...

  4. CTFHub - Web(六)

    命令注入: 1.进入页面,测试127.0.0.1, 关键代码: <?php $res = FALSE; if (isset($_GET['ip']) && $_GET['ip'] ...

  5. 攻防世界 - Web(一)

    baby_web: 1.根据题目提示,初始页面即为index,将1.php改为index.php,发现依然跳转成1.php,尝试修改抓包,出现如下回显, 2.在header中获取flag, flag: ...

  6. Ubuntu16.04安装MySQL8.0

    1.Ubuntu换源(阿里云) sudo cp /etc/apt/sources.list /etc/apt/sources.list.baksudo vi /etc/apt/sources.list ...

  7. linux系统中set、env、export关系

    set 用来显示shell变量(包括环境变量.用户变量和函数名及其定义),同时可以设置shell选项来开启调试.变量扩展.路径扩展等开关env 用来显示和设置环境变量export 用来显示和设置导出到 ...

  8. 二十七:XSS跨站之代码及httponly绕过

    httponly:如果给某个 cookie 设置了 httpOnly 属性,则无法通过 JS 脚本 读取到该 cookie 的信息,但还Application 中手动修改 cookie,所以只是在一定 ...

  9. 1.5V升压3V集成电路升压芯片

    干电池1.5V升压3V的升压芯片,适用于干电池升压产品输出3V供电 1.5V输入时,输出3V,电流可达500MA. PW5100是一款效率大.10uA低功耗 PW5100输入电压:0.7V-5V PW ...

  10. 前端PDF文件转图片方法

    第一步:先下载pdfjs,网址:PDF下载地址,再引入到项目中,我是标签直接引用的 <script src="pdfjs/build/pdf.js"></scri ...