这里为什么会报错?
package hashcat3$6;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class catText {
public static void main(String[] args) {
Set<Cat> set = new HashSet<Cat>();
Cat c1 =new Cat("花花",2,"英短");
Cat c2 = new Cat("凡凡",10,"中华田园猫");
set.add(c2);
set.add(c1);
Iterator<Cat> it =set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("**************************************");
Cat c3 = new Cat("凡凡",10,"中华田园猫");
set.add(c3);
Cat c4 = new Cat("花花二代",2,"英国短毛猫");
set.add(c4);
it=set.iterator();
// while (it.hasNext()){
// System.out.println(it.next());
// }
// System.out.println("***************************************");
// if (set.contains(c4)){
// System.out.println("查询成功");
// System.out.println(c2);
// }else {
// System.out.println("找不到");
// }
// System.out.println("******************找不到测试********************");
// boolean temp =false;
// Cat cat =null ;
// it=set.iterator();
// while (it.hasNext()){
// cat =(Cat)it.next();//将it中的元素取出并转换为Cat类型
// if (cat.getName().equals("慕课")){
// temp =true;
// break;
// }
// }
// if (temp == true){
// System.out.println("找到了");
// System.out.println(cat);
// }else {
// System.out.println("没有找到");
// }
// System.out.println("******************找不到测试********************");
// Cat cat1 = null;
// boolean a = false;
// while (it.hasNext()){
// cat1=(Cat)it.next();
// if(cat1.getName().equals("慕课")){
// a= true;
// }
// }
// if (a){
// System.out.println("查询到数据");
// System.out.println(cat1);
//
// }else {
// System.out.println("没有找到该猫的信息 ");
// }
for (Cat cat:set){
if(("花花二代").equals(cat.getName())){
set.remove(cat);
}
}
for (Cat cat:set){
System.out.println(cat);
}
}
}package hashcat3$6;
import java.util.Objects;
public class Cat {
private String name ;
private int mouth;
private String species;
public Cat(String name, int mouth, String species) {
this.name = name;
this.mouth = mouth;
this.species = species;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cat)) return false;
Cat cat = (Cat) o;
return mouth == cat.mouth &&
Objects.equals(name, cat.name) &&
Objects.equals(species, cat.species);
}
@Override
public int hashCode() {
return Objects.hash(name, mouth, species);
}
@Override
public String toString() {
return "Cat{" +
"姓名='" + name + '\'' +
", 年龄=" + mouth +
", 品种='" + species + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMouth() {
return mouth;
}
public void setMouth(int mouth) {
this.mouth = mouth;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
}
4
收起
正在回答
1回答
同学你好,建议检查下删除的是否是集合中的最后一个元素,若不是则会出现上述异常,可在remove后增加关键字break,用于删除元素后直接跳出循环,不再进行后续的集合遍历操作。
1、java.util.ConcurrentModificationException异常出现的原因:在使用上述循环(增强for循环)遍历集合的过程中对集合中的元素进行了删除,并且删除后又继续遍历集合,故会出现异常。但如果修改和删除的是集合的最后一个元素就不会出现这个问题。
2、视频中未报错是因为在案例集合中花花二代恰巧是最后一个元素。
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星