2015-03-30 100 views
0

我想在對象數組上使用二分搜索。我正在使用對象,因爲對於一個實例,我可能有一組字符串或ints。我目前堅持實施我的compareTo方法,並不確定下一步會是什麼。 這裏是我迄今爲止 -二進制搜索CompareTo Java

public static int binarySearch(Object[] items, Comparable target, int first, int last){ 

    if(first > last) 
     return -1; // Base case for unsuccessful search 
    else{ 
     int middle = (first + last)/2; // Next probe index. 
     int compResult = target.compareTo(items[middle]); 
     if(compResult == 0) 
      return middle; // Base case for unsuccessful search. 
     else if (compResult <0) 
      return binarySearch(items, target, first, middle -1); 
     else 
      return binarySearch(items, target, middle + 1, last); 
    } 
} 
public static int binarySearch(Object[] items, Comparable target){ 
    return binarySearch(items, target, 0, items.length -1); 
} 
@Override 
public int compareTo(T obj) { 

    return 0; 
} 
public static void main(String[] args){ 
String[] names = {"Caryn", "Debbie", "Dustin", "Elliot", "Jacquie", "Jonathan", "Rich"}; 

    int myName = binarySearch(names, "Dustin"); 

當我打電話,我得到一個錯誤的binarySearch它說,在類型FiveThree方法的binarySearch(Object []對象,可比)不適用的參數(字符串[],字符串)。我知道它是因爲我的CompareTo現在是空的,但我不知道如何製作「達斯汀」或其他參數,我把第二個比較而不是字符串。另外,如果我在名稱前面投射對象,它只會將它識別爲一個對象而不是對象[]。
謝謝。

+0

爲什麼使用原始類型而不是泛型? – RealSkeptic 2015-03-30 21:19:39

+0

我想這對我來說是不好的,所以我應該把它改成像列表 names = Arrays.asList(「Caryn」,「Debbie」,「Dustin」,「Elliot」,「Jacquie」,「Jonathan」,「Rich 「); – jumpman8947 2015-03-30 21:32:37

+0

@ jumpman8947不,RealSkeptic意味着你不應該使用原始類型「Comparable」。使用數組很好。 – 2015-03-30 21:36:21

回答

0

您的解決方案基本上可行。

我想在對象數組上使用二分搜索。我使用的對象,因爲我可能有一組字符串或整數

這表明您應該使用泛型而不是Object[]。如果這樣做,則必須使用Integer[]而不是int[],因爲Java泛型不適用於基本類型。

無需編寫compareTo方法,因爲StringInteger已經實現了Comparable

我將Object替換爲T(其中T extends Comparable<T>),它只是工作。它應該打印2

public static <T extends Comparable<T>> int binarySearch(T[] items, T target, int first, int last){ 

    if(first > last) 
     return -1; // Base case for unsuccessful search 
    else{ 
     int middle = (first + last)/2; // Next probe index. 
     int compResult = target.compareTo(items[middle]); 
     if(compResult == 0) 
      return middle; // Base case for unsuccessful search. 
     else if (compResult <0) 
      return binarySearch(items, target, first, middle -1); 
     else 
      return binarySearch(items, target, middle + 1, last); 
    } 
} 

public static <T extends Comparable<T>> int binarySearch(T[] items, T target){ 
    return binarySearch(items, target, 0, items.length -1); 
} 

public static void main(String[] args) { 
    String[] names = {"Caryn", "Debbie", "Dustin", "Elliot", "Jacquie", "Jonathan", "Rich"}; 

    int myName = binarySearch(names, "Dustin"); 
    System.out.println(myName); 
} 
+0

謝謝,我知道我很接近我只是想知道是否必須對CompareTo做任何額外的實現,或者如果我不得不改變其他東西。 – jumpman8947 2015-03-30 21:42:14

+0

當試圖修復它在我的電腦上時,我得到「方法binarySearch(T [],T)類型FiveThree 不適用於參數(字符串[],字符串)」出於某種原因,它只能取名和「達斯汀「作爲唯一的字符串。 – jumpman8947 2015-03-30 22:10:12

+0

我的歉意,我寫了一個接口,並命名它可比較,這是搞砸了,我刪除它,你的代碼的作品。太感謝了。 – jumpman8947 2015-03-30 22:14:06