indexOf
for more details. You should use binary search in your add
method to efficiently find out if a value is already in your list. If so, and if your list is not allowing duplicates, then you shouldn't add the value again. If the value isn't in the list, you need to insert it, and binary search also efficiently helps you discover where it should be inserted. You also need to use binary search in your indexOf
method to efficiently search for elements. Binary search works only if the array is sorted. Again, look at the example calls in the indexOf
section to see how Arrays.binarySearch
is called, what it returns, and how to use its returned value properly.
Arrays.binarySearch
(or some other method of the Arrays
class)?
If all of that still doesn't work, you may need to download the Arrays.java
file located on the Homework page under the files for this assignment. Save it into the same directory as your SortedIntList
and ArrayIntList
classes, compile it, and then try to use Arrays.binarySearch
as before. If it still doesn't work, contact your TA.
System.out.println
commands to check for invalid states.