I made a method that search through an array list of objects. Then if the searchKey is found in the array list it will print this certain item.
Here is how I iterate through the array list if it contains the searchKey, but I just realized that it is impossible to compare a string and an object.
for(int x = 0; x < Student.students.size(); x ++){
if(Student.students.contains(searchKey))
System.out.print(Student.students.get(x));
}
Here's how I create the constructor and array list.
String firstName, lastName, course, yearLevel, gender;
Student(String firstName, String lastName, String course, String yearLevel, String gender)
{
this.firstName = firstName;
this.lastName = lastName;
this.course = course;
this.yearLevel = yearLevel;
this.gender = gender;
}
static ArrayList<Student> students = new ArrayList<Student>();
-
please share the List instanciation and class of the object into itazro– azro05/13/2022 06:21:44Commented May 13, 2022 at 6:21
-
1what are you searching for? first name, last name, course, ... or gender? any of these? exact or partial match?user16320675– user1632067505/13/2022 07:32:02Commented May 13, 2022 at 7:32
2 Answers 2
You need to compare the one property; also you can use a for-each loop to simplify the code
for(Student s : Student.students){
if(s.getName().equals(searchKey))
System.out.print(s);
}
Note :
When you use a condition Student.students.contains(searchKey)
in a loop, and it doesn't use the iteration variable that means there is a problem
-
I posted how I create the list. I also try the method you just mentioned, but it wont work as it is still comparing a string to an objectSilly Bean– Silly Bean05/13/2022 07:09:07Commented May 13, 2022 at 7:09
-
1@SillyBean: No, it's comparing the name of the student to the search key...Jon Skeet– Jon Skeet05/13/2022 07:33:55Commented May 13, 2022 at 7:33
-
You can also use a stream,
students.stream().filter(s -> searchKey.equals(s.getName()).forEach(System.out::println)
.daniu– daniu05/13/2022 09:51:38Commented May 13, 2022 at 9:51 -
@SillyBean what is
searchKey
a string or a Student ?azro– azro05/14/2022 07:25:51Commented May 14, 2022 at 7:25
You haven't defined what 'contains' means here but I'm going to assume that it means a Student contains the key if it appears as a substring in any of its String members. So let's start with a method that does that. We can define this as part of the Student class itself.
public class Student {
.... other stuff ....
/**
* Return true if any of the properties of this Student
* contain the given substring, false otherwise
*/
public boolean contains(String s) {
// consider addressing null cases - omitting for simplicity
return firstName.contains(s) ||
lastName.contains(s) ||
course.contains(s) ||
yearLevel.contains(s) ||
gender.contains(s);
}
}
Now you can iterate over your List and invoke this method to find the matches. Note that you need to handle the case that multiple Students may match a given search key (or none may match). So I would suggest collecting the results in a separate List. One does not generally iterate over Lists via the index. This example uses an enhanced for-loop (aka for-each).
public List<Student> findMatches(List<Student> students, String key) {
List<Student> found = new ArrayList<>();
for (Student s : students) {
if (s.contains(key)) {
found.add(s);
}
}
return found;
}
This is a good case for using the Stream API.
public List<Student> findMatches(List<Student> students, String key) {
return students.stream()
.filter(s -> s.contains(key))
.collect(Collectors.toList());
}