我有一个包含五个对象的arrayList,我现在必须找到姓氏为Smith的所有元素,并有另一个类来打印它。我该怎么做呢? 我有三个班级:学生班,教室和包含考试方法的司机班。我必须制作一个名为的方法: 在名为“class room”的类中使用withName(String name),然后通过考试模式打印它。我该怎么做呢?
首先,我在学生班中创建了一个getName()方法:
public String getName(){
return name;
}
然后我尝试了:
public Student withName(String name){
for( Student s : students){
if(s != null && m.getName().contains("Smith"))
return s;
}
return null;
}
答案 0 :(得分:0)
这是你问题的答案, 但是我不会用勺子喂你解决方案。
答案 1 :(得分:0)
尝试这样做:
// find all elements where the surname is ...
public List<Student> withName(String name) {
List<Student> auxList = new ArrayList<Student>();
for (Student s : students) {
if (s != null && s.getName().contains("Smith"))
auxList.add(s);
}
return auxList;
}