Java 如果找到特定的数字,如何使此打印为真或假?
我想进行二进制搜索以在我的 如果找到arraylist并打印false或true 我希望我的targetValue查看它们是否存在于arr列表中,并打印true或falseJava 如果找到特定的数字,如何使此打印为真或假?,java,Java,我想进行二进制搜索以在我的 如果找到arraylist并打印false或true 我希望我的targetValue查看它们是否存在于arr列表中,并打印true或false public static void main(String[] args) throws IOException { { int arr[] = {10,20,30,40}; int targetValue[]= {10,25,40} }
public static void main(String[] args) throws IOException {
{
int arr[] = {10,20,30,40};
int targetValue[]= {10,25,40}
}
}
这是我的二进制搜索代码
public static boolean binarySearch(int[] arr, int n) {
int first = 0;
int last = arr.length-1;
int mid;
while (first <= last){
mid = first + (last - first) / 2;
if (n == arr[mid]) return true;
else if (n < arr[mid]) last = mid - 1;
else first = mid + 1;
}
return false;
}
公共静态布尔二进制搜索(int[]arr,int n){
int first=0;
int last=arr.length-1;
int mid;
(首先假设数组是按升序排序的,这应该可以:
publicstaticvoidmain(字符串[]args)引发IOException{
{
int arr[]={10,20,30,40};
int targetValue[]={10,25,40};
int指数=0;
while(索引
I现在,在if/else上不使用大括号是合法的……但不要这样做:循环目标值,对每个值进行二进制搜索并打印结果。在网上找到其他人二进制搜索并不难。我的老师在powerpoint上给我看了一个。我一直在尝试不同的编写方法以更好地理解它。稍后我将不得不修改它,以便我可以在文本文件中搜索值。因此,不,我没有作弊。好的,谢谢你的输入托马斯,罗布谢谢你的帮助。[索引]的功能是什么?我唯一不明白的是什么?