这里为什么会报错?

这里为什么会报错?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
    }
}

https://img1.sycdn.imooc.com//climg/62737b7d09264b3b09240287.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,建议检查下删除的是否是集合中的最后一个元素,若不是则会出现上述异常,可在remove后增加关键字break,用于删除元素后直接跳出循环,不再进行后续的集合遍历操作。

1、java.util.ConcurrentModificationException异常出现的原因:在使用上述循环(增强for循环)遍历集合的过程中对集合中的元素进行了删除,并且删除后又继续遍历集合,故会出现异常。但如果修改和删除的是集合的最后一个元素就不会出现这个问题。

2、视频中未报错是因为在案例集合中花花二代恰巧是最后一个元素。

祝学习愉快~

  • ZZ6459418 提问者 #1

    集合中的元素不应该是无序的吗?这里是怎么判断是不是最后一个的?

    2022-05-05 15:32:15
  • 好帮手慕小小 回复 提问者 ZZ6459418 #2

    同学你好,是的,set集合中的元素是无序的,故这里只能说课程中删除的这个元素是恰巧存储在了集合中的最后一个位置。

    祝学习愉快~

    2022-05-05 15:43:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码