|NO.Z.00046|——————————|BigDataEnd|——|Java&集合类库.V03|——|Java.v03|collection集合.v02|
一、案例题目:
### --- 案例题目:
——> 如何使用迭代器实现toString方法的打印效果?
二、for each循环(重点)
### --- 基本概念
——> Java5推出了增强型for循环语句,可以应用数组和集合的遍历。
——> 是经典迭代的“简化版”。
### --- 语法格式
——> for(元素类型 变量名 : 数组/集合名称) {
——> 循环体;
——> }
### --- 执行流程
——> 不断地从数组/集合中取出一个元素赋值给变量名并执行循环体,直到取完所有元素为止。
三、编程代码
package com.yanqi.task14;
import java.util.Objects;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
四、编程代码
package com.yanqi.task14;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class CollectionTest {
public static void main(String[] args) {
// 1.准备一个Collection集合并打印
//Collection c1 = new Collection(); // 接口不能实例化,也就是不能创建对象
// 接口类型的引用指向实现类的对象,形成了多态
Collection c1 = new ArrayList();
// 自动调用toString方法,调用ArrayList类中的toString方法,默认打印格式为:[元素值1, 元素值2, ...]
System.out.println("集合中的元素有:" + c1); // [啥也没有]
System.out.println("--------------------------------------------------------");
// 2.向集合中添加单个元素并打印
boolean b1 = c1.add(new String("one"));
System.out.println("b1 = " + b1); // true
System.out.println("集合中的元素有:" + c1); // [one]
b1 = c1.add(Integer.valueOf(2));
System.out.println("b1 = " + b1); // true
System.out.println("集合中的元素有:" + c1); // [one, 2]
b1 = c1.add(new Person("zhangfei", 30));
System.out.println("b1 = " + b1); // true
// 打印集合中的所有元素时,本质上就是打印集合中的每个对象,也就是让每个对象调用对应类的toString方法
System.out.println("集合中的元素有:" + c1); // [one, 2, Person{name='zhangfei', age=30}]
System.out.println("--------------------------------------------------------");
// 3.向集合中添加多个元素并打印
Collection c2 = new ArrayList();
c2.add("three"); // 常量池
c2.add(4); // 自动装箱机制
System.out.println("c2 = " + c2); // [three, 4]
// 将c2中的所有元素全部添加到集合c1中,也就是将集合c2中的元素一个一个依次添加到集合c1中
b1 = c1.addAll(c2);
// 表示将集合c2整体看做一个元素添加到集合c1中
//b1 = c1.add(c2);
System.out.println("b1 = " + b1);
// [one, 2, Person{name='zhangfei', age=30}, three, 4] [one, 2, Person{name='zhangfei', age=30}, [three, 4]]
System.out.println("c1 = " + c1);
System.out.println("--------------------------------------------------------");
// 4.判断集合中是否包含参数指定的单个元素
b1 = c1.contains(new String("one"));
System.out.println("b1 = " + b1); // true
b1 = c1.contains(new String("two"));
System.out.println("b1 = " + b1); // false
b1 = c1.contains(Integer.valueOf(2));
System.out.println("b1 = " + b1); // true
b1 = c1.contains(Integer.valueOf(3));
System.out.println("b1 = " + b1); // false
// contains方法的工作原理是:Objects.equals(o, e),其中o代表contains方法的形式参数,e代表集合中的每个元素
// 也就是contains的工作原理就是 拿着参数对象与集合中已有的元素依次进行比较,比较的方式调用Objects中的equals方法
// 而该方法equals的工作原理如下:
/*
public static boolean equals(Object a, Object b) { 其中a代表Person对象,b代表集合中已有的对象
return (a == b) || (a != null && a.equals(b));
元素包含的第一种方式就是:Person对象与集合中已有对象的地址相同
第二种方式就是:Person对象不为空,则Person对象调用equals方法与集合中已有元素相等
}
*/
// 当Person类中没有重写equals方法时,则调用从Object类中继承下来的equals方法,比较两个对象的地址 false
// 当Person类中重写equals方法后,则调用重写以后的版本,比较两个对象的内容 true
b1 = c1.contains(new Person("zhangfei", 30));
System.out.println("b1 = " + b1); // true false
System.out.println("--------------------------------------------------------");
// [one, 2, Person{name='zhangfei', age=30}, three, 4]
System.out.println("c1 = " + c1);
// 5.判断当前集合中是否包含参数指定集合的所有元素
Collection c3 = new ArrayList();
c3.add(4);
System.out.println("c3 = " + c3); // [4]
// 判断集合c1中是否包含集合c3中的所有元素
b1 = c1.containsAll(c3);
System.out.println("b1 = " + b1); // true
c3.add("five");
System.out.println("c3 = " + c3); // [4, five]
// 判断集合c1中是否包含集合c3中的所有元素,只有集合c3中的所有元素都在集合c1中出现才会返回true,否则都是false
b1 = c1.containsAll(c3);
System.out.println("b1 = " + b1); // false
// 笔试考点
System.out.println("c2 = " + c2); // [three, 4]
b1 = c1.containsAll(c2);
System.out.println("b1 = " + b1); // true false
// 判断集合c1中是否拥有集合c2这个整体为单位的元素
b1 = c1.contains(c2);
System.out.println("b1 = " + b1); // false true
System.out.println("--------------------------------------------------------");
// 6.计算两个集合的交集并保留到当前集合中
System.out.println("c2 = " + c2); // [three, 4]
System.out.println("c3 = " + c3); // [4, five]
// 也就是让集合自己和自己取交集,还是自己,也就是当前集合中的元素没有发生改变
b1 = c2.retainAll(c2);
System.out.println("b1 = " + b1); // false 表示当前集合中的元素没有发生改变
System.out.println("c2 = " + c2); // [three, 4]
// 计算集合c2和c3的交集并保留到集合c2中,取代集合c2中原有的数值
b1 = c2.retainAll(c3);
System.out.println("b1 = " + b1); // true 当前集合的元素发生了改变
System.out.println("c2 = " + c2); // [4]
System.out.println("c3 = " + c3); // [4, five]
System.out.println("--------------------------------------------------------");
// 7.实现集合中单个元素的删除操作
// [one, 2, Person{name='zhangfei', age=30}, three, 4]
System.out.println("c1 = " + c1);
// 删除参数指定的单个元素
b1 = c1.remove(1);
System.out.println("b1 = " + b1); // false
// [one, 2, Person{name='zhangfei', age=30}, three, 4]
System.out.println("c1 = " + c1);
b1 = c1.remove("one");
System.out.println("b1 = " + b1); // true
// [2, Person{name='zhangfei', age=30}, three, 4]
System.out.println("c1 = " + c1);
// remove方法的工作原理:Objects.equals(o, e)
b1 = c1.remove(new Person("zhangfei", 30));
System.out.println("b1 = " + b1); // true
// [2, three, 4]
System.out.println("c1 = " + c1);
System.out.println("--------------------------------------------------------");
// 8.实现集合中所有元素的删除操作
System.out.println("c3 = " + c3); // [4, five]
// 从集合c1中删除集合c3中的所有元素,本质上就是一个一个元素进行删除,有元素则删除,否则不删除
b1 = c1.removeAll(c3);
System.out.println("b1 = " + b1); // true
// [2, three]
System.out.println("c1 = " + c1);
System.out.println("c3 = " + c3); // [4, five]
// 笔试考点 删除整体对象c3
b1 = c1.remove(c3);
System.out.println("b1 = " + b1); // false
System.out.println("c1 = " + c1); // [2, three]
System.out.println("--------------------------------------------------------");
// 9.实现集合中其它方法的测试 ctrl+n 可以直接搜索并打开类的源码 使用ctrl+f12可以搜索类中的方法
System.out.println("集合中元素的个数为:" + c1.size()); // 2
System.out.println(0 == c1.size() ? "集合已经空了": "集合还没有空"); // 没有空
System.out.println(c1.isEmpty()? "集合已经空了": "集合还没有空"); // 没有空
// 清空集合中的所有元素
c1.clear();
System.out.println("集合中元素的个数为:" + c1.size()); // 0
System.out.println(0 == c1.size() ? "集合已经空了": "集合还没有空"); // 已经空了
System.out.println(c1.isEmpty()? "集合已经空了": "集合还没有空"); // 已经空了
// 准备两个集合并判断是否相等
Collection c4 = new ArrayList();
c4.add(1);
c4.add(2);
System.out.println("c4 = " + c4); // [1, 2]
Collection c5 = new ArrayList();
c5.add(1);
c5.add(2);
c5.add(3);
System.out.println("c5 = " + c5); // [1, 2, 3]
// 判断是否相等
b1 = c4.equals(c5);
System.out.println("b1 = " + b1); // true false
System.out.println("--------------------------------------------------------");
// 10.实现集合和数组类型之间的转换 通常认为:集合是用于取代数组的结构
// 实现集合向数组类型的转换
Object[] objects = c5.toArray();
// 打印数组中的所有元素
System.out.println("数组中的元素有:" + Arrays.toString(objects)); // [1, 2, 3]
// 实现数组类型到集合类型的转换
Collection objects1 = Arrays.asList(objects);
System.out.println("集合中的元素有:" + objects1); // [1, 2, 3]
}
}
五、编程代码
package com.yanqi.task14;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class CollectionPrintTest {
public static void main(String[] args) {
// 1.准备一个Collection集合并放入元素后打印
Collection c1 = new ArrayList();
c1.add("one");
c1.add(2);
c1.add(new Person("zhangfei", 30));
// 遍历方式一: 自动调用toString方法 String类型的整体
System.out.println("c1 = " + c1); // [one, 2, Person{name='zhangfei', age=30}]
System.out.println("------------------------------------------------");
// 2.遍历方式二:使用迭代器来遍历集合中的所有元素 更加灵活
// 2.1 获取当前集合中的迭代器对象
Iterator iterator1 = c1.iterator();
/*
// 2.2 判断是否有元素可以访问
System.out.println(iterator1.hasNext()); // true
// 2.3 取出一个元素并指向下一个
System.out.println("获取到的元素是:" + iterator1.next()); // one
System.out.println(iterator1.hasNext()); // true
System.out.println("获取到的元素是:" + iterator1.next()); // 2
System.out.println(iterator1.hasNext()); // true
System.out.println("获取到的元素是:" + iterator1.next()); // Person{name='zhangfei', age=30}
System.out.println(iterator1.hasNext()); // false
System.out.println("获取到的元素是:" + iterator1.next()); // 编译ok,运行发生NoSuchElementException没有这样的元素异常
*/
while (iterator1.hasNext()) {
System.out.println("获取到的元素是:" + iterator1.next());
}
System.out.println("------------------------------------------------");
// 由于上个循环已经使得迭代器走到了最后,因此需要重置迭代器
iterator1 = c1.iterator();
// 3.使用迭代器来模拟toString方法的打印效果
StringBuilder sb1 = new StringBuilder();
sb1.append("[");
while (iterator1.hasNext()) {
Object obj = iterator1.next();
// 当获取的元素是最后一个元素时,则拼接元素加中括号
if (!iterator1.hasNext()) {
sb1.append(obj).append("]");
} else {
// 否则拼接元素加逗号加空格
sb1.append(obj).append(",").append(" ");
}
}
// [one, 2, Person{name='zhangfei', age=30}]
System.out.println("c1 = " + sb1);
System.out.println("------------------------------------------------");
// 4.不断地去获取集合中的元素并判断,当元素值为"one"时则删除该元素
iterator1 = c1.iterator();
while (iterator1.hasNext()) {
Object obj = iterator1.next();
if("one".equals(obj)) {
iterator1.remove(); //使用迭代器的remove方法删除元素没问题
//c1.remove(obj); // 使用集合的remove方法编译ok,运行发生ConcurrentModificationException并发修改异常
}
}
System.out.println("删除后集合中的元素有:" + c1); // [2, Person{name='zhangfei', age=30}]
System.out.println("------------------------------------------------");
// 5.使用 for each结构实现集合和数组中元素的遍历 代码简单且方法灵活
// 由调试源码可知:该方式确实是迭代器的简化版
for (Object obj : c1) {
System.out.println("取出来的元素是:" + obj);
}
int[] arr = new int[] {11, 22, 33, 44, 55};
for (int i : arr) {
System.out.println("i = " + i);
i = 66; // 修改局部变量i的数值,并不是修改数组中元素的数值
}
System.out.println("数组中的元素有:" + Arrays.toString(arr));
}
}
六、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53886:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task14.CollectionTest
集合中的元素有:[]
--------------------------------------------------------
b1 = true
集合中的元素有:[one]
b1 = true
集合中的元素有:[one, 2]
b1 = true
集合中的元素有:[one, 2, Person{name='zhangfei', age=30}]
--------------------------------------------------------
c2 = [three, 4]
b1 = true
c1 = [one, 2, Person{name='zhangfei', age=30}, three, 4]
--------------------------------------------------------
b1 = true
b1 = false
b1 = true
b1 = false
b1 = true
--------------------------------------------------------
c1 = [one, 2, Person{name='zhangfei', age=30}, three, 4]
c3 = [4]
b1 = true
c3 = [4, five]
b1 = false
c2 = [three, 4]
b1 = true
b1 = false
--------------------------------------------------------
c2 = [three, 4]
c3 = [4, five]
b1 = false
c2 = [three, 4]
b1 = true
c2 = [4]
c3 = [4, five]
--------------------------------------------------------
c1 = [one, 2, Person{name='zhangfei', age=30}, three, 4]
b1 = false
c1 = [one, 2, Person{name='zhangfei', age=30}, three, 4]
b1 = true
c1 = [2, Person{name='zhangfei', age=30}, three, 4]
b1 = true
c1 = [2, three, 4]
--------------------------------------------------------
c3 = [4, five]
b1 = true
c1 = [2, three]
c3 = [4, five]
b1 = false
c1 = [2, three]
--------------------------------------------------------
集合中元素的个数为:2
集合还没有空
集合还没有空
集合中元素的个数为:0
集合已经空了
集合已经空了
c4 = [1, 2]
c5 = [1, 2, 3]
b1 = false
--------------------------------------------------------
数组中的元素有:[1, 2, 3]
集合中的元素有:[1, 2, 3]
Process finished with exit code 0
七、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53892:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task14.CollectionPrintTest
c1 = [one, 2, Person{name='zhangfei', age=30}]
------------------------------------------------
获取到的元素是:one
获取到的元素是:2
获取到的元素是:Person{name='zhangfei', age=30}
------------------------------------------------
c1 = [one, 2, Person{name='zhangfei', age=30}]
------------------------------------------------
删除后集合中的元素有:[2, Person{name='zhangfei', age=30}]
------------------------------------------------
取出来的元素是:2
取出来的元素是:Person{name='zhangfei', age=30}
i = 11
i = 22
i = 33
i = 44
i = 55
数组中的元素有:[11, 22, 33, 44, 55]
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通