开发者

How do find a element that is less then another element?

I'm trying to set up a Binary search program that uses strings instead of integers. Problem is that I don't know how to make an array of numbers that are less then a string value.

For example

string array less than string value.

/**
   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 t开发者_高级运维o
      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;
   }
}


Your problem is the comparison operator (==). The comparison operator is only well defined for primitive data types in Java. String is a class (not a primitive data type). Therefore, you will need to use String's equals(String) method to compare them.

If you want to compare them as numbers, then you will need to parse them into Integers. For this, you can use Integer.parseInt(String) and then compare the Integers.


The first comparison:

if (numbers[middle] == value)

should not use the == operator. Remember you're comparing String objects. You should either use the equals method or compareTo.

The next comparison you have is:

// 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)

According to your comments you're checking to see if value is in the lower half of the array, but your code is comparing an array element to the next array element. To match the comments it should be:

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

Also, please note that you will get some strange looking results when comparing Strings that represent numbers the way you're doing. The compareTo method compares Strings lexicographically. This means, for example, that "5" will evaluate to be greater than "11".


This works fine for primitive data type. Not for String objects.

== is used to check whether the reference of two objects are same or not. "==" never compares the content of two objects.

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

strName1 == strName2 is false. Since they are referring to two different objects.

You can use equals method to do comparison.

if (strName2 .equals(strName2 )) {
    System.out.println("Me and Me are same :P");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜