How do I find ID in an Array List of student objects

user12869445 :
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);
      }
  }  
}

For the addStudentInLine method, I need to add the id to the integer Arraylist inLine if the id exists in inClass arraylist.(only students in a class can receive help) If the id does not exist in inClass arrayList, return false. I'm not sure how to traverse through the inClass arrayList in order to see if the id already exist because inClass arrayList consists of student objects. I've tried .contain() method but it also seems to not work. can someone please help?

Deadpool :

You should reverse the condition in addStudentInLine method, If any Student id matching to ID add that ID to inLine list and return true or else just return `false

public boolean addStudentInLine(Integer id) { 

   for(int i = 0; i<inClass.size(); i++) {

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

     }  
    return false;
 }  

You can also use for-each block

 public boolean addStudentInLine(Integer id) { 

   for(Student stu : inClass) {

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

     }  
    return false;
 }  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i get the list of ids of feetype followed by student id

How do I get an id from a student in a list and display it the student's information on another page? Ruby on Rails

how to find in an Array of Objects by id?

How do I find single attribute value in the list of objects in scala

How do I find the smallest variable from a list of objects? (JAVA)

How do I find the intersection between an array of objects and a collection in MongoDB?

How do I sort an objects array by id and name?

How do I list Javascript array of objects in HTML

How do I create a react list component from an array of objects?

How do I convert a named list to an array of objects

How do i sort arrayList via student ID

Javascript - filtering a list: how can I find an intersection between an array with objects including array and an array?

How to find object by id in array of objects?

How can I sort an array of objects by ID?

How do you store a list of objects in an array?

How do I merge array of objects with array of ObjectIds into one unique list in MongoDB?

How do I sort card objects by their id

How can i list an array of objects in Javascript?

How do I find in Java a specific element, for printing, in a list of objects with multiple elements?

How to find all id's to an array from array of objects - Javascript

How can I find all objects as objects in the list in Django?

How do I create a list of objects in Kotlin?

How do I fetch a list of objects in grails?

How do I get a list of webapi objects?

How do I display a list of objects in an Exception?

How can i do a list of objects in javascript?

NextJS How do I use the id from a list of objects and redirect to a page based on that?

How do I traverse an array of objects and find the number of times and element name occurs using Lodash

How do I find the averages of similar objects in an array and output them into one object?