I’m a relative newbie to Java and am building an application for my programming course where users can enter, remove, search or edit a list of Students (an object which consists of an integer ID number, a String last name, and a double GPA). So far, I have had success with allowing the user to enter, remove, or edit entries. Right now, the list of 100+ students are in an ArrayList (each as a Student object with the three parameters).
My problem is that I don’t know an efficient way to allow the user to search for a specific student based on ID number, last name, or GPA (and possibly GPA range in the future). I have been studying Big-O notation and would like to use a binary search because it would be great practice as well as a better choice if the Student list continues to grow. So far, for binary searches, we are using the high/low/mid method with a while loop (if that is any indication of my current skill level).
So here is my question:
What is an efficient way to search for a specific student based on this criteria? I am not looking for code or answers to my dilemma. I’m really looking for the names of techniques or tricks that Java programmers use to do such a thing, especially if those techniques can be used with other object-oriented programming languages.
For binary search, you’d need the list to be sorted by the criteria you are searching with.
You could maintain three different lists, sorted by the three different criteria you need:
Note that when you insert a new Student to your ArrayList, you should also insert that Student into the other, sorted ArrayLists in its correct place. (You can use binary search to do this, of course.)