0

假设我有一个ArrayList<Student> contains 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>
        }
     }
}
4

3 回答 3

2

假设您的学生是这样建模的(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!..
  }
}
于 2013-09-13T03:21:53.737 回答
1

我相信Student is class,你正在创建列表Student

使用类(您的案例 Student 类)中实现ArrayList的方法来进行等于比较。equals

您可以调用包含列表的方法来获取匹配的对象

喜欢,

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

  public Student(String name, String city, String school) {
      this.name = name;
      this.city = city;
      this.school = school;
  }

   //getters & setters..
  public String setSchool(String school) {
      this.school = school;
  }

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

  public boolean equals(Object other) {
      if (other == null) return false;
      if (other == this) return true;

      if (!(other instanceof Student)) return false;
      Student s = (Student)other;

      if (s.getSchool().equals(this.getSchool())) {
          return true; // here you compare school name
      } 
      return false;
  }

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

你的数组列表是这样的

ArrayList<Student> studentList = new ArrayList<Student>();  

Student s1 = new Student(x, y, z);
Student s2 = new Student(a, b, c);  

studentList.add(s1);
studentList.add(s2);

Student s3 = new Student(x, y, z); //object to search

if(studentList.contains(s3)) {
    System.out.println(s3.toString()); //print object if object exists;
} // check if `studentList` contains `student3` with city `y`.It will internally call your equals method to compare items in list.

或者,

您可以简单地迭代对象studentList并比较项目

for(Student s : studentList) {
  if(s.getSchool().equals(schoolToSearch)) {
    // print object here!..

  }
}

或者,正如您评论的那样,

public void displayStudentBasedOnSchool(String school){
    for(int i = 0; i < studentList.size(); ++i) {
        if(studentList.get(i).getSchool().equals(school)) {
            System.out.println(studentList.get(i).toString()); // here studentList.get(i) returns Student Object.
        }
    }
}

或者,

ListIterator<Student> listIterator = studentList.listIterator(); //use list Iterator

while(listIterator.hasNext()) {
    if(iterator.next().getSchool().equals(school)) {
        System.out.println(listIterator.next());
        break;
    }
}

甚至,

int j = 0;
while (studentList.size() > j) {
    if(studentList.get(j).getSchool().equals(school)){
        System.out.println(studentList.get(j));
        break;
    }
    j++;
}

所以现在你有一组选项

  1. for-loop
  2. for-each环形
  3. while环形
  4. iterator
于 2013-09-13T03:36:44.257 回答
0

我可能会使用Google 的Guava库。
看看这个问题:过滤 Java 集合的最佳方法是什么?它为您的问题提供了许多出色的解决方案。

于 2013-09-13T08:17:57.893 回答