I tried using a binary search with an Arraylist and it gave me this message:
The method binarySearch(List>, T) in the type Collections is not applicable for the arguments (ArrayList, String)
Here's the code:
ArrayList <Object> a = new ArrayList <Object> ();
String date = JOptionPane.showInputDialog(null, "Please enter the date.")
int index = Collections.binarySearch(a, date);
The binary search should return the position of a specific date from an array (a) of several dates. What am I doing wrong?
To perform a binary search, your
Collection
must be sorted. In order to sort something, this something needs to have a well-defined order. That is, what theComparable<T>
interface is for.Object
does not implementComparable<Object>
and therefore aList
ofObject
cannot be sorted (ther are no criteria to sort them). Please look at the API for mor details.