2011-03-17 158 views
0

我想設置一個二進制搜索程序,使用字符串而不是整數。問題是我不知道如何創建一個小於一個字符串值的數組。如何找到一個小於另一個元素的元素?

例如

字符串數組小於字符串值。

/** 
    The StringBinarySearcher class provides a public static 
    method for performing a binary search on an String array. 
*/ 



public class StringBinarySearcher 
{ 
    /** 
     The search method performs a binary search on an String 
     array. The array is searched for the number passed to 
     value. If the number is found, its array subscript is 
     returned. Otherwise, -1 is returned indicating the 
     value was not found in the array. 
     @param numbers The array to search. 
     @param value The value to search for. 
    */ 



    public static int search(String[] numbers, String value) 
    { 
     int first;  // First array element 
     int last;  // Last array element 
     int middle;  // Mid point of search 
     int position; // Position of search value 
     boolean found; // Flag 

     // Set the inital values. 
     first = 0; 
     last = numbers.length - 1; 
     position = -1; 
     found = false; 

     // Search for the value. 
     while (!found && first <= last) 
     { 
     // Calculate mid point 
     middle = (first + last)/2; 

     // If value is found at midpoint... 
     if (numbers[middle] == value) 
     { 
      found = true; 
      position = middle; 
     } 

     // else if value is in lower half... 
     // needs array to be less then the string value?, without using equality regulators 
     else if (numbers[middle].compareTo(numbers[middle +1]) > 0) 
      last = middle - 1; 
     // else if value is in upper half.... 
     else 
      first = middle + 1; 
     } 

     // Return the position of the item, or -1 
     // if it was not found. 
     return position; 
    } 
} 
+0

爲什麼你認爲你需要一個數組數組來對你的字符串數組進行二分搜索? – 2011-03-17 01:33:09

+0

順便說一句,'如果(數字[中] ==值)'是錯誤的比較,使用'如果(數字[中] .equals(價值))' – MByD 2011-03-17 01:34:42

+0

可以排序字符串數字,與整數? – user663428 2011-03-17 01:36:03

回答

1

第一個比較:

if (numbers[middle] == value) 

應該使用==操作。記住你正在比較String對象。您應該使用equals方法或compareTo

你的下一個比較:

// else if value is in lower half... 
// needs array to be less then the string value?, without using equality regulators 
else if (numbers[middle].compareTo(numbers[middle +1]) > 0) 

根據您的意見你檢查,看看是否value是在陣列的下半部分,但你的代碼是一個數組元素比較下一個數組元素。爲了匹配的評論應該是:

else if (value.compareTo(numbers[middle]) < 0) 

而且,請注意,比較代表號碼,你正在做的方式字符串時,你會得到一些奇怪的妝效。 compareTo方法按字典順序比較字符串。這意味着,例如,「5」將評估爲大於「11」。

+0

else if(value.compareTo(numbers [middle])<0)不會僅比較小於值的一個元素。 – user663428 2011-03-17 01:47:45

+0

@ user663428:如果「值」(按字母順序)小於數組的中間元素,則返回'true'。 – 2011-03-17 01:50:22

2

你的問題是比較運算符(==)。 Java中的基本數據類型只能很好地定義比較運算符。字符串是一個類(不是原始數據類型)。因此,您需要使用String的equals(String)方法來比較它們。

如果您想將它們作爲數字進行比較,那麼您需要將它們解析爲整數。爲此,您可以使用Integer.parseInt(String)然後比較整數。

+0

好吧,我忘記了,我應該知道更好。 – user663428 2011-03-17 01:37:36

0

這對原始數據類型正常工作。不適用於String對象。

==用於檢查兩個對象的引用是否相同。 「==」永遠不會比較兩個對象的內容。

String strName1 = "Me"; 
String strName2 = new String("Me"); 

strName1 == strName2是錯誤的。因爲他們指的是兩個不同的對象。

您可以使用equals方法進行比較。

if (strName2 .equals(strName2)) { 
    System.out.println("Me and Me are same :P"); 
} 
相關問題