Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a specific object in arrayList

How can I get a List<Student> data in School list?

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");

I only have school name. But it couldn't use indexOf method. because that's only works same object.

that means I need to get School object not school name.

how do I find School object.

here are DataTypes.

Student.java enter image description here

School.java enter image description here

like image 850
bubu uwu Avatar asked Jun 04 '26 01:06

bubu uwu


1 Answers

It seems like you are trying to find a specific school in the list of schools. If this is not what you are trying to do, please let me know.

Here's how I would do it:

public School findSchool(String schoolName)
{
    // Goes through the List of schools.
    for (School i : schools)
    {
        if (i.getName.equals(schoolname))
        {
            return i;   
        }   
    }
    return null;
}
like image 51
Hatefiend Avatar answered Jun 06 '26 15:06

Hatefiend