Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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 公共布尔removeStudent(int-id)_Java - Fatal编程技术网

Java 公共布尔removeStudent(int-id)

Java 公共布尔removeStudent(int-id),java,Java,请帮我修正错误 public boolean removeStudent(int id) { for (Student student : this) { if ((student.getID()) == (id)) return true; id.remove(); } return false; } 错误:无法取消对int的引用。 我正在尝试根据id从列表中删除学生。但是.remove与ints不兼容 你

请帮我修正错误

public boolean removeStudent(int id)
{
    for (Student student : this)
    {
        if ((student.getID()) == (id)) 
        return true;
        id.remove(); 
    }
    return false; 
}
错误:无法取消对int的引用。
我正在尝试根据id从列表中删除学生。但是.remove与ints不兼容

你不是想调用
student.remove()
或者类似的东西吗


此外,该代码前面的
返回true
行不会命中该代码。

此代码看起来不太好。首先:remove调用是错误的,remove总是被调用,因为if语句没有用括号封装。

id
是一种
int
,是一种基本类型,因此它没有任何方法

id.remove(); //will never compile
将代码更改为

for (int x =0; x < this.size();x++) {
    //your if should contain the removal and the return statements
    if ((this.get(x).getID()) == (id)) {
        this.remove(this.get(x)); 
        return true;
    }
}
return false;
for(int x=0;x
重新输入代码,您将看到问题:

public boolean removeStudent(int id)
{
    for (Student student : this)
    {
        if ((student.getID()) == (id)) {
            return true;
        }
        id.remove(); 
    }
    return false; 
}
看看你现在在做什么:一旦你击中一个ID匹配的学生,你会立即跳出方法,返回true。在此之前,您将删除所有迭代的学生。i、 e.在找到匹配的学生之前,您将删除所有学生

我觉得这不太正常

我打赌您要做的是:删除所有具有匹配ID的学生。如果有任何学生被删除,请返回true,否则返回false

如果是,请尝试理解这段代码: (我不是给你直接的答案。如果你能理解这段代码中发生了什么,那么你可以很容易地修复你的代码)

希望
b
是一个集合(更准确地说,是可伸缩的)或数组

另一个问题是,id是一个整数,您希望id.remove()能做什么?您正在告诉一个整数执行“remove()”

我假设您正在执行类似于
this.studentList.remove(id)
this.studentList.remove(student)

this.remove(student)
?还是别的什么?没有足够的上下文。
// print out only odd numbers in the list, and return true if there is any.
boolean printOdd(List<Integer> numbers) {
  boolean oddFound = false;
  for (int i : numbers) {
    if ((i % 2) != 0) {
        System.out.println("Odd Number Found: " + i);
        oddFound = true;
    }
  }
  return oddFound;
}
 for (Type a : b) {...}