Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当arraylist中的某个名称多次出现在列表中时,告知删除该名称的程序_Java - Fatal编程技术网

Java 当arraylist中的某个名称多次出现在列表中时,告知删除该名称的程序

Java 当arraylist中的某个名称多次出现在列表中时,告知删除该名称的程序,java,Java,当ArrayList中的某个名称在列表中出现两次时,告知删除该名称的程序。这是计划的一部分II需要以下方面的帮助: if (list.contains(nameToFind) && int i = 1; i < THIS IS WHERE I NEED HELP) System.out.println("Hey, that name is written twice on the list, edit or delete one of the

当ArrayList中的某个名称在列表中出现两次时,告知删除该名称的程序。这是计划的一部分II需要以下方面的帮助:

if (list.contains(nameToFind) && int i = 1; i < THIS IS WHERE I NEED HELP)
                System.out.println("Hey, that name is written twice on the list, edit or delete one of them!");
            }
 else {
                System.out.println(" This name is only written once");
写一个方法来计算列表包含nameToFind的次数。。。在Java8+中,可以使用过滤器和计数。像

if (list.stream().filter(s -> s.equals(nameToFind)).count() > 1) {
    System.out.println("Hey, that name is written twice on the list, "
            + "edit or delete one of them!");
}
在早期版本中,可以使用迭代列表并统计给定字符串(如


在条件语句中声明i是行不通的。另外,如果您正在检查i==1的位置,那么将确保只返回列表中的第二项。You want istatic int count(List<String> al, String name) { int count = 0; for (String s : al) { if (s.equals(name)) { count++; } } return count; }
if (count(list, nameToFind) > 1) {
    // ...
}