![](/img/trans.png)
[英]How do I compare attributes of an object in an ArrayList against Scanner input?
[英]how do I compare a input string “BBBB” with the schools in the above ArrayList?
假设我有一个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!..
}
}
我相信Student is class
,您正在创建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++;
}
所以现在您有了一些选择
for-loop
for-each
循环 while
循环 iterator
我可能会使用Google的Guava库。
看一下这个问题: 过滤Java集合的最佳方法是什么? 它为您的问题提供了许多出色的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.