質問

my String Buffer has "_ @ ." .I have Binary Search method that return false for char '.'. i can not find how to check speacial character in Binary Search.Please help to find out this issue .

役に立ちましたか?

解決 2

Correct me if I am wrong, but binary search is used on sorted arrays.

You are trying to perform binary search on array of chars _ @ ..
In terms of ASCII codes, that looks like this: [32][95][32][64][32][46].

So, when you call your function:
Low: 0 - Middle: 2 - High: 5
Seach: 46 - Actual: 32
Low: 3
Low: 3 - Middle: 4 - High: 5
Seach: 46 - Actual: 32
Low: 5
Low: 5 - Middle: 5 - High: 5
Found!!!
true

However, when you take out the empty spaces and run it over "_@."

Low: 0 - Middle: 1 - High: 2
Seach: 46 - Actual: 64
High: 0
Low: 0 - Middle: 0 - High: 0
Seach: 46 - Actual: 95
High: -1
Not found!!!
false

To summarize:

If you are absolutely sure you'll be running your search over a sorted array, then this is OK.

However, like in your case, if you can't guarantee that you'll have sorted array, then don't use binary search.

他のヒント

you called wrong function. change like this..

public static void main(String[] args) {
    StringBuffer b = new StringBuffer();
    b.append(" _ @ .");
    boolean isValid = false;
    isValid = isValueThere(b, '.');
    System.out.print(isValid);
}

and isValueThere(b, '.'); return true.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top