6 Replies Latest reply on Mar 1, 2010 6:46 AM by 843793

    Collections.binarySearch with ArrayList<String> - basic question

      Why can't I do this? These errors showed up when I upgraded to Eclipse Ganymede (and I guess Java 6)...
      They were never errors before.
      ArrayList<String> subjects = new ArrayList<String>(lstSubjects.getSelected());
      Comparable<String> c = "k";
      int index = Collections.binarySearch(subjects, c);
      index = Collections.<String>binarySearch(subjects, c);
      When I declare c explicitly as a String I get no error. Obviously this code is ridiculous but it is simply
      illustrative. The point is I have classes which implement Comparable and I want to be able to search lists of
      these classes with a String, or as in this case a list of Strings with these classes. One of the advantages of
      the Comparable interface for me is that I can compare, sort, and search lists of different class types. This
      seems to be impossible now, or else I'm just confused (probably the latter).

      The errors are:
      [Line 3] The method binarySearch(List<? extends Comparable<? super T>>, T) in the type 
      Collections is not applicable for the arguments (ArrayList<String>, Comparable<String>)
      [Line 4] The parameterized method <String>binarySearch(List<? extends Comparable<? super String>>,
      String) of type Collections is not applicable for the arguments (ArrayList<String>, Comparable<String>)
      Since String is an instance of Comparable<String> I expect at least Line 4 to be correct. But I'm still
      a bit out to lunch when it come to generics I guess...