gpt4 book ai didi

java - 如何将输入字符串 “BBBB” 与上面 ArrayList 中的学校进行比较?

转载 作者:行者123 更新时间:2023-12-01 23:35:26 25 4
gpt4 key购买 nike

假设我有一个 ArrayList<Student>包含 4 Students(name , city, school) 。例如:

 1. John   Nevada   BBBB
2. Mary Ander AAAA
3. Winn Arcata CCCC
4. Ty Artes BBBB

如果用户输入“BBBB”,则显示::

 1. John    Nevada   BBBB
2. Ty Artes BBBB

我的问题是如何将输入字符串“BBBB”与上面ArrayList中的学校进行比较?感谢你们提供的任何帮助!

public class Student
{
private String name;
private String city;
private String school;

/**
* Constructor for objects of class Student
*/
public Student(String name, String city, String school)
{
this.name = name;
this.city = city;
this.school = school;
}

public String getSchool(String school)
{
return this.school = school;
}

public String toString()
{
return "Name: " + name + "\tCity: " +city+ "\tSchool: "+school;
}


}


public class AllStudent
{
// instance variables - replace the example below with your own
private ArrayList<Student> listStudent = new ArrayList<Student>();

/**
* Constructor for objects of class AllStudent
*/
public AllStudent() throws IOException
{

//variables
// read an input file and save it as an Arraylist
fileScan = new Scanner (new File("students.txt");
while(fileScan.hasNext())
{
//.......
listStudent.add(new Student(name,city,school);
}
//now let user enter a school, the display name, city, school of that student.
//i am expecting something like below...
public void displayStudentBasedOnSchool(String school)
{
for (i = 0; i < listStudent.size(); i++)
{
//what should i put i here to comapre in input School with school in the listStudent>
}
}
}

最佳答案

假设您的学生的模型如下(AAAA、BBBB 值存储在 blah 字段中):

public class Student {

private String name;
private String state;
private String blah;

//getters & setters..

}

最简单(不是最有效的方法)就是循环数组列表并将查询字符串与 blah 字段的值进行比较

for(Student s : studentList) {
if(s.getBlah().equals(queryString)) {
// match!..
}
}

关于java - 如何将输入字符串 “BBBB” 与上面 ArrayList 中的学校进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777708/

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