Here's example:
public class Course {
private String name;
private Student[] students;
private int capacity=40;
private int numberOfStudents;
public Course(String name){
this.name=name;
}
public Course(String name, int capacity){
this.name= name;
this.capacity= capacity;
}
public int getNumberOfStudents(){
return numberOfStudents;
}
public String getCourseName(){
return name;
}
public Student[] getStudents(){
return students;
}
public boolean addStudents(Student newStudent){
if(numberOfStudents < capacity){
students[numberOfStudents++] = newStudent;
return true;
}
return false;
}
}
I'm trying to add a new student to the Student[] students array. I wrote the code above. In the Student class, every student has a unique id.
The problem is that while I am adding newStudent, I want check if newStudent already exists in the class. To do that I should use id property of students because every student has its own unique id. How can I add it to do if statement?
HashMapinstead of an array for this.