【发布时间】:2020-01-13 09:21:36
【问题描述】:
所以我有一个类类型的列表。例如,我有一个 ClassRoom 类和一个 Student 类。
我创建了一个学生数组列表:ArrayList<Student> studList = new ArrayList<>();
我有一个添加学生的方法,所以索引0被占用:
public void addStudent(int studID, String studName, int studPhoneNum){
Student stud = new Student(studName, studID, studPhoneNum);
studList.add(stud);
System.out.println(studList);
}
现在我想要的是在学生变量中搜索特定值。例如,学生由姓名、ID 和电话号码表示。如何遍历该学生列表以查找具有特定电话号码的学生,然后打印他或他的电话号码?
这是函数:
public void sendMessage(int phone){
for (Student s : studList) {
studList.get(studPhoneNum);
\\ then print the Student the phone number belongs to..
}
}
我确实在 Student 类中设置了一个 studPhoneNum 变量。
【问题讨论】:
-
if(s.getPhonenumber == phone) -
谢谢!我不敢相信我之前没有找到这个。