At work today, I encountered the problem of finding a string in the array, the Java code is as follows:
A binary method is used to find the data, and if found, returns True, otherwise it returns false;
Arrays that are computed using the binary method must already be sorted in ascending order
list--an array to query
name--to find strings
public static Boolean isenabled (List list, String name) {
int iindex = 0; Something that corresponds to a pointer
int iStart = 0; //
int iend = List.size ()-1;
int icount = 0;
while (true) {
Iindex = (IStart + iend)/2;
if (List.get (Iindex)). ToString (). toLowerCase (). CompareTo (
Name.tolowercase ()) > 0) {
Iend = Iindex;
else if ((List.get (Iindex)). ToString (). toLowerCase (). CompareTo (
Name.tolowercase ()) < 0) {
IStart = Iindex;
} else {
Break
}
/**
* SYSTEM.OUT.PRINTLN ("icount is" +icount);
* SYSTEM.OUT.PRINTLN ("Comparative Value:" + (List.get (Iindex)). ToString (). toLowerCase ());
* SYSTEM.OUT.PRINTLN ("To be compared value:" +name.tolowercase ());
* SYSTEM.OUT.PRINTLN ("comparison Result:" + (List.get (Iindex)). ToString (). toLowerCase (). CompareTo (Name.tolowercase ()));
*/
icount++;
Prevents a dead loop from being found, even if no corresponding value exists and only the number of times the length of the array is compared
if (Icount > List.size ())
return false;
}
return true;
}