gpt4 book ai didi

java - 获取 arrayList 中的特定对象

转载 作者:太空狗 更新时间:2023-10-29 15:40:50 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com