简体   繁体   English

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

[英]how do I compare a input string “BBBB” with the schools in the above ArrayList?

Let say I have an ArrayList<Student> contains 4 Students(name , city, school) . 假设我有一个ArrayList<Student>包含4个Students(name , city, school) For example: 例如:

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

If user enter “BBBB” then it displays: : 如果用户输入“ BBBB”,则显示::

 1. John    Nevada   BBBB
 2. Ty      Artes    BBBB

My question is that how do I compare a input string “BBBB” with the schools in the above ArrayList ? 我的问题是如何将输入字符串“ BBBB”与上面的ArrayList的学校进行比较? Thank you for any help that you guys would provide! 感谢您提供的任何帮助!

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>
        }
     }
}

Assuming your student is modelled like this (AAAA, BBBB values are stored in blah field): 假设您的学生是这样建模的(AAAA,BBBB值存储在blah字段中):

public class Student {

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

  //getters & setters..

}

The simplest (not most efficient way) is just to loop the array list and compare the query string with value of blah field 最简单(不是最有效的方法)只是循环数组列表,并将查询字符串与blah字段的值进行比较

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

I believe Student is class and you are creating list of Student 我相信Student is class ,您正在创建Student列表

The ArrayList uses the equals method implemented in the class (your case Student class) to do the equals comparison. ArrayList使用equals的类(你的情况下,学生类)实现的方法做等于比较。

You can call contains methods of list to get matching object 您可以调用包含列表的方法来获取匹配的对象

Like, 喜欢,

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;
  }
}

Your array list would be like this 您的数组列表将如下所示

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.

Or, 要么,

You can simply iterate object in studentList and compare items 您可以简单地迭代studentList对象并比较项目

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

  }
}

Or, as you commented , 或者,正如您评论的那样,

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.
        }
    }
}

Or, 要么,

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

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

or even, 甚至,

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

So now you have set of options 所以现在您有了一些选择

  1. for-loop
  2. for-each loop for-each循环
  3. while loop while循环
  4. iterator

I would probably use Guava library from Google. 我可能会使用Google的Guava库。
Take a look at this question: What is the best way to filter a Java Collection? 看一下这个问题: 过滤Java集合的最佳方法是什么? It provides many excelent solutions for your problem. 它为您的问题提供了许多出色的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM