简体   繁体   English

检查数组是否包含值,如果没有,则将其添加到arraylist-并发修改异常-Java

[英]check if array contains values, if not then add to arraylist - Concurrent Modification Exception - Java

I am passing a String name to a method, if that name isn't in the array then it gets added. 我正在将String name传递给方法,如果该name不在数组中,则将其添加。

I tried this first but got Concurrent Modification Exception 我首先尝试了此方法,但并发修改异常

List<String> people = new ArrayList<>();

public void addName(String name) {
    if (people.isEmpty()) {
        people.add(name);
    } else {
        for (String s : people) {
            if (s.equals(name)) {
                System.out.println("dont add");
            } else {
                people.add(name);
            }
        }
    }
}

After reading on forums I learned you have to use iterator to avoid this. 在论坛上阅读后,我了解到您必须使用迭代器来避免这种情况。 I tried it and fixed the Concurrent Modification Exception , but the player gets added even though I stated them not to be added if they exist in the array, I do get this output "name exists" when I pass a name already existing in the list, but then it runs "name added" as well so dont understand why this is happening 我尝试了该问题并修复了Concurrent Modification Exception ,但是即使我声明如果数组中存在它们也不添加它们,也将添加播放器,当我传递列表中已经存在的名称时,我的确会得到此输出“ name exist” ,但是它也会同时运行“已添加名称”,所以不明白为什么会这样

if (people.isEmpty()) {
    people.add(name);
} else {
    String name2 = null;
    for (Iterator<String> it = people.iterator(); it.hasNext(); ) {
        String element = it.next();
        if (element.equals(name)) {
            String message = "name exists";
            System.out.println(message);
            name2 = null;
        } else if (!element.equals(name)) {
            System.out.println("Name added");
            name2 = name;
        }
    }
    if (name2 != null) {
        people.add(name2);
    }
}

You can do it simply by: 您可以通过以下方式简单地做到这一点:

public void addName(String name) {
    if (!people.contains(name)) {
        people.add(name);
    }
}

It sounds like you are trying to re-invent a set. 听起来您正在尝试重新创造一套。

Set<String> names = new TreeSet<>(); // Or set of your choice
names.add("Joe"); // Set contents ["Joe"]
names.add("Bob"); // Set contents ["Joe", "Bob"];
names.add("Joe"); // Set contents ["Joe", "Bob"];

If you want to have the printlines 如果您想要打印线

if (names.add(name) {
  System.out.println("Added name " + name);
} else {
  System.out.println("Already added " + name);
}

I reckon you need to break out of your loop once you find that the name already exists. 我认为一旦发现名称已经存在,就需要打破循环。 Otherwise name2 might get set to null and then set to a non-null value again. 否则, name2可能会设置为null ,然后再次设置为非null值。

For example: 例如:

           for (Iterator<String> it = people.iterator(); it.hasNext(); ) {
                String element = it.next();
                if (element.equals(name)) {
                    String message = "name exists";
                    System.out.println(message);
                    name2 = null;
                    break;
                } else if(element.equals(name)==false) {
                    System.out.println("Name added");
                    name2 = name;
                }
            }

But do have a look at contains since this is a much much cleaner approach! 但是请查看contains因为这是一种更清洁的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM