我正在尝试编写一个测试ArrayList的二进制搜索的程序,但是我一直在收到我不知道如何修复的错误。
这是我的二进制搜索代码:
import java.util.ArrayList;
public class BinarySearchTest {
public static void main(String[] args)
{
ArrayList<Integer> searchable = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++)
searchable.add(i);
int someNumber = 7;
int index = binarySearch(searchable, someNumber, 0, searchable.size()));
System.out.println(someNumber + ((index == -1) ? " is not in the array" : (" is at index " + index)));
}
public static boolean binarySearch(ArrayList<Integer> searchable, int check, int lo, int hi)
{
int first = 0;
int last = searchable.size() - 1;
int mid;
while(first <= last)
{
mid = first + (last - first) / 2;
if(check == searchable.get(i).getTotal())
{
return true;
}else if(searchable.compareTo(searchable.get(i)) < 0)
{
last = mid - 1;
}else
{
first - mid + 1;
}
}
return false;
}
}
返回的错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type of the expression must be an array type but it resolved to ArrayList<Integer>
The type of the expression must be an array type but it resolved to ArrayList<Integer>
at BinarySearchTest.binarySearch(BinarySearchTest.java:24)
at BinarySearchTest.main(BinarySearchTest.java:13)
我认为这与我的binarySearch方法有关,需要调整它以使用ArrayList而不仅仅是常规数组。