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


I am trying to set up a binary search program which uses an integer instead of strings. The problem is that I Do not know how the array of numbers is less than the string value that is less.

For example

string string less than string value.

  / ** The StringBasicirectureer provides a public static method to perform a binary search on the string string array. * / Public class StringBinarySearcher {/ ** The search method performs a binary search on a string array. The array is searched for the number passed by the value, if the number is received, its array membership is returned. Otherwise, -1 has been returned, indicating that the value was not found in the array. @Paramel Number to search for value @ Palam value to search for value * / Public Static Ent Search (string [] number, string value) {int first; // First Array element ending last; // final array element middle middle; // the middle point of the search point; // found the position of the search value boolean; // Flag // Set the default value first = 0; Final = numbers Long - 1; Condition = -1; Found = false; // search for value while calculating (! Found & amp; first & lt; = final) {// middle dot middle = (first + last) / 2; // If the value is found at the middle point ... if (numbers [middle] == value] {found = true; Position = mediocre; } // If the value is in the lower half ... // without the use of similarity regulators, the array needs to have less string values? If (numbers [middle] .compareTo (number [mid 1])> 0) final = medium - 1; // and if the value is in the upper half ... and the first = medium + 1; } // Return the status of the item, or -1 / if it was not found. Return status; }}    

First comparison:

  Number [middle] == value)   

no == operator should be used. Remember that you are comparing string objects. You should either use the method or

You have the next comparison:

  // else if the value is in the lower half ... // If the required array is used without the equality regulators, then the string value Is less? If (numbers [middle] .comparareTo (number [middle 1]) gt; 0)   

is checking to see if your value Array is in the lower half, but your code compares an array element with the next array element. To match the comments it should be:

  else if (value.compareTo (number [middle]) <0 / code>  

Also, please note When comparing strings, you will get some weird results which represent the numbers by you. Comparison method defines the strings physically, for example, "5" will evaluate more than "11".

Comments