2011-03-17 161 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"); 
} 
相关问题