java - 获取 arrayList 中的特定对象

标签 java android arraylist

如何在学校列表中获取 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 对象。

这里是数据类型。

学生.java enter image description here

学校.java enter image description here

最佳答案

您似乎正在尝试在学校列表中查找特定学校。如果这不是您想要做的,请告诉我。

这是我的做法:

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/

相关文章:

java - Swing 和 Activity

Java 8如何使用数组中的id从数组列表中删除项目

java - 为什么 ArrayList<>.add() 不起作用?

java - 迭代和处理 ArrayList

java - Java中如何比较时间戳是否在给定时间段的范围内

java - 如何在游戏完成第一关后自动进入下一关?

java - 如何在java中使用apache poi更新docx文件的元数据?

android - 海拔不适用于 ImageView

java - 二进制 XML 文件行 #24 : Error inflating class com. google.android.material.navigation.NavigationView

Android 列 '_id' 不存在