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 - 如何获取文本文件中的句子并将其放入数组列表Java?

java - 如何在 Netbeans 中设置观察点?

java - KeyListener 无法使用 Applet

android - 使用登录(身份验证)访问远程 pdf 文件

java - android的ListActivity类什么时候调用setContentView()?

android - addAccountExplicitly 成功,但未在设置/帐户中显示任何帐户

Java流删除前三个元素

java - 错误: incompatible types Object Could not be converted [ArrayList]

java - 如果源代码是隐藏的,为什么还需要防御性编程?

java - 套接字编程 : Is extra '\n' added in the golang client or the Java server?