Need help writing a binary search algorithm that takes in a List<T> generic, and returns the index

I need help with writing this binary search algorithm involved with generics in java. I just can’t figure out how to implement the overridden compareTo() method.
The code you see below cannot be changed the way the class has been implemented.
I have to figure out how to implement the code inside of the method called indexOf(T t).
In the test class this is how the code is written to pass in the parameters to the indexOf() method.
In the assert equals is the expected value and the param on the right is the object calling upon the method that I need to implement.

public void findsAValueAtTheEndOfAnArray() { List<Integer> sortedList = Collections.unmodifiableList(Arrays.asList(1, 3, 4, 6, 8, 9, 11)); EvaluationService.BinarySearch<Integer> search = new EvaluationService.BinarySearch<>(sortedList);  assertEquals(6, search.indexOf(11)); } 
static class BinarySearch<T>  implements Comparable<T> {         private List<T> sortedList;                  public int indexOf(T t) {                      return 0;         }                           public BinarySearch(List<T> sortedList) {             super();             this.sortedList = sortedList;         }          public List<T> getSortedList() {             return sortedList;         }          public void setSortedList(List<T> sortedList) {             this.sortedList = sortedList;         }          @Override         public int compareTo(T arg0) {                          return 0;         } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.