标记的语法错误,错位的构造 - 二分搜索方法中的错误
Syntax error on token(s), misplaced construct(s) - Error in binary search methods
这是我的代码。我找不到问题所在。请帮我。我在 Eclipse 中有这个错误:
Syntax error on token(s), misplaced construct(s)
在递归方法
的行旁边写了错误
if (arr[mid] == item) {
但在我看来,我所有的牙套和构造器都很好。我在构造函数上也有错误:
Missing method return type.
我认为构造函数不应该有 return 类型,对吗?
public final class IntegerBinSearch {
/**
* Default constructor.
*/
private IntegerBinarySearch() {
}
/**
* Returns the index of the item that you are looking for, -1
* if the item isn't in the array.
* The array must be sorted.
*
* @param array the array that will be searched.
* @param item the item that the method will try to find.
* @return the index of the item's location, returns -1 if not found.
*/
public static int binarySearch(Integer[] array, int item) {
int high = array.length - 1;
int low = 0;
int mid = (high + low) / 2;
for (int i = array.length; i > 0; i /= 2) {
if (array[mid] == item) {
return mid;
} else if (array[mid] > item) {
high = mid - 1;
mid = (high + low) / 2;
} else {
low = mid + 1;
mid = (high + low) / 2;
}
}
return -1;
}
/**
* Returns the index of the location of the given item, else returns -1.
*
* @param array array of Integers that will be searched.
* @param item item that will be searched for.
* @param low lower bound of the array's contents to search.
* @param high upper bound of the array's contents to search.
* @return returns the index of the location of the given item, returns -1 if item is not found.
*/
public static int binarySearchRecursive(Integer[] arr, int item, int low, int high) {
int mid = (high + low) / 2;
if (arr[mid] == item) {
return mid;
}
else(arr[mid] > item) {
return binarySearchRecursive(arr, item, low, mid - 1);
} else {
return binarySearchRecursive(arr, item, mid + 1, high);
}
}
}
当你使用多个条件时,你应该写 else if 语句
你应该替换
else (arr[mid] > item) {
和
else if (arr[mid] > item) {
并且构造函数名称必须与 class 名称相同,正如@Adam 所说
请尝试
这是我的代码。我找不到问题所在。请帮我。我在 Eclipse 中有这个错误:
Syntax error on token(s), misplaced construct(s)
在递归方法
的行旁边写了错误if (arr[mid] == item) {
但在我看来,我所有的牙套和构造器都很好。我在构造函数上也有错误:
Missing method return type.
我认为构造函数不应该有 return 类型,对吗?
public final class IntegerBinSearch {
/**
* Default constructor.
*/
private IntegerBinarySearch() {
}
/**
* Returns the index of the item that you are looking for, -1
* if the item isn't in the array.
* The array must be sorted.
*
* @param array the array that will be searched.
* @param item the item that the method will try to find.
* @return the index of the item's location, returns -1 if not found.
*/
public static int binarySearch(Integer[] array, int item) {
int high = array.length - 1;
int low = 0;
int mid = (high + low) / 2;
for (int i = array.length; i > 0; i /= 2) {
if (array[mid] == item) {
return mid;
} else if (array[mid] > item) {
high = mid - 1;
mid = (high + low) / 2;
} else {
low = mid + 1;
mid = (high + low) / 2;
}
}
return -1;
}
/**
* Returns the index of the location of the given item, else returns -1.
*
* @param array array of Integers that will be searched.
* @param item item that will be searched for.
* @param low lower bound of the array's contents to search.
* @param high upper bound of the array's contents to search.
* @return returns the index of the location of the given item, returns -1 if item is not found.
*/
public static int binarySearchRecursive(Integer[] arr, int item, int low, int high) {
int mid = (high + low) / 2;
if (arr[mid] == item) {
return mid;
}
else(arr[mid] > item) {
return binarySearchRecursive(arr, item, low, mid - 1);
} else {
return binarySearchRecursive(arr, item, mid + 1, high);
}
}
}
当你使用多个条件时,你应该写 else if 语句
你应该替换
else (arr[mid] > item) {
和
else if (arr[mid] > item) {
并且构造函数名称必须与 class 名称相同,正如@Adam 所说
请尝试