如何在学校列表中获取 List<Student>
数据?
List<School> schools = new ArrayList<School>();
School school_aaa = new School();
school_aaa.setName( "aaa" );
Student student_aaa_001 = new Student();
student_aaa_001.setName( "aaa_001" );
student_aaa_001.setAge( 17 );
student_aaa_001.setId( 21345678 );
Student student_aaa_002 = new Student();
student_aaa_002.setName( "aaa_002" );
student_aaa_002.setAge( 13 );
student_aaa_002.setId( 6789876 );
List<Student> students = new ArrayList<Students>();
students.add( student_aaa_001 );
students.add( student_aaa_002 );
school_aaa.setStudents( students );
schools.add("aaa");
我只有学校名称。 但它不能使用 indexOf 方法。 因为那只适用于同一个对象。
这意味着我需要获取学校对象而不是学校名称。
如何找到 School 对象。
这里是数据类型。
最佳答案
您似乎正在尝试在学校列表中查找特定学校。如果这不是您想要做的,请告诉我。
这是我的做法:
public School findSchool(String schoolName)
{
// Goes through the List of schools.
for (School i : schools)
{
if (i.getName.equals(schoolname))
{
return i;
}
}
return null;
}
关于java - 获取 arrayList 中的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35008222/