![](/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.