我在txt文件中有一个排序的整数列表(在线公开托管),我需要让用户通过二进制搜索给出一个在整数列表中搜索的数字。
我参加算法课程的介绍,我是Java的新手。
现在我已经得到了这些错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 52, Size: 22
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at BinSearch4.main(BinSearch4.java:67)
* while循环(下面列出)是第67行
似乎无法弄清问题在这里。我也相信,即使我工作的一些事情也可以更有效地实施。任何人都可以提供的帮助将不胜感激。
/* Binary Search Algorithm
*
* - Import a sorted list of integers of an unknown length
*
* - Ask the user for a number to search for (searchValue)
*
* - Run a Binary Search on the list of integers without simply using binarySearch
*
* - Return the index of an occurrence of the search number (searchValue)
*
* - Or -1 if the target is not found.
*
*
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
import java.util.ArrayList;
public class BinSearch4 {
public static void main(String[] args) throws IOException {
URL url = new URL("http://m.uploadedit.com/ba3a/1423978916244.txt");
ArrayList<Integer> arrList = new ArrayList<Integer>();
int searchValue;
int low;
int high;
int mid;
int NOT_FOUND = -1;
Scanner in = new Scanner(System.in);
System.out.println("Enter your number to search for:");
searchValue = in.nextInt();
System.out.println(searchValue);
try {
Scanner scr = new Scanner(url.openStream());
while (scr.hasNextLine()) {
int i = scr.nextInt();
arrList.add(i);
}
scr.close();
System.out.println(arrList);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
low = arrList.get(0);
System.out.println(low);
high = arrList.get(arrList.size() - 1);
System.out.println(high);
mid = (low + high) / 2;
while (low <= high && arrList.get(mid) != searchValue) {
if (arrList.get(mid) < searchValue) {
low = mid + 1;
} else {
high = mid - 1;
}
mid = (low + high) / 2;
}
if (low > high) {
mid = NOT_FOUND;
}
System.out.println(mid);
}
}
文本文件中的数字列表是: [-2,3,6,9,11,22,24,31,35,43,48,52,62,65,69,70,73,83,86,90,100,107]
答案 0 :(得分:0)
更改您的行:
low = arrList.get(0);
System.out.println(low);
high = arrList.get(arrList.size() - 1);
要
low = 0;
System.out.println(low);
high = arrList.size() - 1;
你获得异常的原因是,如果你的列表中有10个元素,并且你在列表中有100 - 110个元素,那么你试图按索引访问列表元素,即列表中不存在的100个元素因为你的列表只包含10个元素,即访问索引9之外的元素(因为数组索引从0开始到n-1),你将获得IndexOutOfBoundsException
。
答案 1 :(得分:0)
二进制搜索
public class BinarySearch {
public int binarySearch(int[] a) {
int start = 0;
int end = a.length - 1;
int searchedValue = 70;
for (int i = 0; i <= a.length; i++) {
int mid = (start + end) / 2;
if (searchedValue == a[mid]) {
return 1;
}
if (searchedValue < a[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
int a[] = { 2, 5, 40, 50, 60, 70 };
BinarySearch binarySearch = new BinarySearch();
System.out.println(binarySearch.binarySearch(a));
}
}
start必须为0,end将为arr.length-1
答案 2 :(得分:0)
感谢android_dev和almas,他们都是正确的。
此处我的原始代码在此处发布:
low = arrList.get(0);
System.out.println(low);
high = arrList.get(arrList.size() - 1);
我正在引用该值,我需要引用索引。因此,“低”&#39;对于像这样的二进制搜索程序,index将始终需要为零。
我现在正常运作的代码是:
low = 0;
System.out.println(low);
high = arrList.size() - 1;