Object Search in ArrayList

I am a relative newbie to Java and am creating an application for my programming course where users can enter, delete, search, or edit the student list (an object that consists of an integer identification number, String last name, and a dual GPA). So far, I have been successful in allowing the user to enter, delete, or edit entries. Right now, a list of 100 students is in an ArrayList (each as a Student object with three parameters).

My problem is that I don’t know an effective way to allow the user to search for a specific student based on their identification number, last name or GPA (and possibly the range of GPA in the future). I studied Big-O notation and would like to use binary search, because it will be a great practice, as well as a better choice if the list of students continues to grow. So far, for binary searches, we have used the high / low / mid method with a while loop (if this is any indicator of my current skill level).

So here is my question:

What is an effective way to find a specific student based on these criteria? I am not looking for code or answers to my dilemma. I'm really looking for the names of those tricks or tricks that Java programmers use, especially if these methods can be used with other object-oriented programming languages.

+5
source share
4 answers

For a binary search, you will need to sort the list by the criteria you are looking for.

You can save three different lists, sorted by three different criteria:

ArrayList<Student> sortedByName;
ArrayList<Student> sortedByGpa;
ArrayList<Student> sortedById;

public List<Student> getStudentByName(String name){
    return binarySearchOnName(sortedByName, name);
}
public List<Student> getStudentByGpa(double gpa){
    return binarySearchOnGpa(sortedByGpa, gpa);
}
...

Note that when you insert a new student into your ArrayList, you must also insert this Student into another, sorted ArrayLists in the right place. (Of course, you can use binary search to do this.)

+2
source

HashMap. , , . - . - ( : ID, lastname GPA), . , () .

:

Map<String, Student> idToStudent;

: "23213233", : "Some Student";

gpa, :

Map<String, List<Student>> lastNameToStudents;

: "", : [ " ", " " ..]

Map<Double, List<Student>> gpaToStudents:

: "2.4", : [ " 1", " 2" ..];

. , Student.

+4

? , TreeSet, HashSet. TreeSet O (log (n)) // ( . HashSet O (1) //, ( ). .

, , , HashMap/TreeMap, . , , . ArrayLists , O (n), (1 , ).

, O (n) . (, 10000 , 100), , . - , - .

+1

, . , , . , , , ID . equals + hashcode. , , ( , ). , :

list.contains(Student);

, , - :

 matchedStudents = new ArrayList<Student>();
    for(Student currentStudent : studentList)  
    {  
        if(currentStudent.getName().equalsIgnoreCase(searchString)  
        {
              matchedStudents.add(currentStudent);  
        }
    }  
      return matchedStudents;

GPA , , , GPA == searchCriteria.

that, as said, it should be possible to write such a function:

function searchStudent(String searchString, String type)  
{    
    //type is one of: GPA, name, ID  
    switch(type)  
    {  
       case gpa:  
       case id:  
       case name:
    }  
}  

which should run you.

+1
source

All Articles