Java 如何在学生对象的数组列表中查找ID 公共类学生{//包含名称和id的学生对象类 字符串名; int-ID; 公立学生(字符串名称,整数ID){ this.name=名称; this.ID=ID; } @凌驾 公共字符串toString(){ 返回名称+“”+ID; } } //另一个类有两个数组列表,一个类中的一个学生数组和一个等待帮助的学生数组 ArrayList inClass=新的ArrayList(); ArrayList inLine=新的ArrayList()//等待帮助的学生的ID 公共布尔addStudentInLine(整数id){ 对于(int i=0;i

Java 如何在学生对象的数组列表中查找ID 公共类学生{//包含名称和id的学生对象类 字符串名; int-ID; 公立学生(字符串名称,整数ID){ this.name=名称; this.ID=ID; } @凌驾 公共字符串toString(){ 返回名称+“”+ID; } } //另一个类有两个数组列表,一个类中的一个学生数组和一个等待帮助的学生数组 ArrayList inClass=新的ArrayList(); ArrayList inLine=新的ArrayList()//等待帮助的学生的ID 公共布尔addStudentInLine(整数id){ 对于(int i=0;i,java,arraylist,Java,Arraylist,您应该在addStudentInLine方法中反转条件,如果有Studentid与id匹配,则将该id添加到inLine列表并返回true,否则只返回'false' public class Student { //student object class containing name and id String name; int ID; public Student(String name, int ID) { this.name = name;

您应该在
addStudentInLine
方法中反转条件,如果有
Student
id与
id
匹配,则将该
id
添加到
inLine
列表并返回
true
,否则只返回'false'

public class Student { //student object class containing name and id
    String name;
    int ID;

    public Student(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    @Override
    public String toString() {
        return name +" "+ ID;
    }
}
//another class with two array lists, an array of students in a class and an array of students waiting for help
ArrayList<Student> inClass = new ArrayList<Student>();
ArrayList<Integer> inLine = new ArrayList<Integer>(); //IDs of students who are waiting for help

public boolean addStudentInLine(Integer id) { 
   for(int i = 0; i<inClass.size(); i++) {
      if (inClass.get(i).ID != id) {
          return false;
      }
      if (inClass.get(i).ID == id) {
          inClass.add(id);
      }
  }  
}

您应该在
addStudentInLine
方法中反转条件,如果有
Student
id与
id
匹配,则将该
id
添加到
inLine
列表并返回
true
,否则只返回'false'

public class Student { //student object class containing name and id
    String name;
    int ID;

    public Student(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    @Override
    public String toString() {
        return name +" "+ ID;
    }
}
//another class with two array lists, an array of students in a class and an array of students waiting for help
ArrayList<Student> inClass = new ArrayList<Student>();
ArrayList<Integer> inLine = new ArrayList<Integer>(); //IDs of students who are waiting for help

public boolean addStudentInLine(Integer id) { 
   for(int i = 0; i<inClass.size(); i++) {
      if (inClass.get(i).ID != id) {
          return false;
      }
      if (inClass.get(i).ID == id) {
          inClass.add(id);
      }
  }  
}

在遍历整个列表之前,您无法确定该用户是否存在于列表中,因此您的抢先退出(使用
false
)将不起作用。但是,如果您确实找到了该学生,您可以提前存在,因为无需继续检查列表。我将删除
if(inClass.get(I).ID!=ID){…}
,添加一个
返回true
;到
if(inClass.get(i).ID==ID){…}
阻塞并在方法末尾添加一个默认值
return false;
只是一个旁注…比较
Integer==Integer
可能会引起其他问题。也许您应该将
id
声明为
int
。在遍历ent之前,您无法确定该用户是否存在于列表中ire列表,所以你先发制人的退出(使用
false
)是不起作用的。但是,如果你找到了学生,你可以提前存在,因为不需要继续检查列表。我会删除
if(inClass.get(I.ID!=ID){…}
,在
if(inClass.get(I.ID==ID){…}中添加
return true
;如果(inClass.get(I.ID==ID){…}
阻塞并在方法末尾添加一个默认值
return false;
只是一个旁注…比较
Integer==Integer
可能会引起其他问题。也许您应该将
id
声明为
int
 public boolean addStudentInLine(Integer id) { 

   for(Student stu : inClass) {

       if (stu.ID.equals(id)) {
           inClass.add(id);
           return true;
        }

     }  
    return false;
 }