filepointer
卡住導致StackOverflowError
的一個點。你能指出我在這裏有什麼錯誤嗎?錯誤的確切是:java.lang.StackOverflowError
Java中文件中遞歸二進制搜索中的StackOverflowError
我正在尋找找到的位置,因爲記錄寬度不固定。
這裏是一段代碼:
private static void binarySearch(RandomAccessFile raf, String searchvalue, Long low, Long high) throws IOException
{
Long middle = (low + high)/2;
Long mreal = null;
if(low > raf.length() -1 || high > raf.length()-1 || low >= high) {
System.out.println("Element not found:"); return ;
}
StringBuilder sb = new StringBuilder();
for(long filePointer = middle; filePointer != -1; filePointer--) {
raf.seek(filePointer);
int readByte = raf.readByte();
if(readByte == 0xA) {
break;
}
sb.append((char)readByte);
}
String lastLine = sb.reverse().toString();
System.out.println(lastLine);
mreal = raf.getFilePointer();
String str = raf.readLine();
System.out.println(str);
String values[] = str.split("\t",-1);
int compared = searchvalue.compareTo(values[fieldindex]);
System.out.println(fieldindex);
if(compared == 0) {
System.out.println("Value found. The other details:");
for(int i=0; i < values.length;i++)
System.out.print("\t" + values[i]);
return;
} else if(compared < 0)
binarySearch(raf,searchvalue,low,mreal-1);
else if(compared > 0)
binarySearch(raf,searchvalue,mreal,high);
}
堆棧跟蹤:在螺紋
異常 「主」 java.lang.StackOverflowError的
>at java.util.regex.Pattern$Node.<init>(Pattern.java:2993)
>at java.util.regex.Pattern$CharProperty.<init>(Pattern.java:3332)
>at java.util.regex.Pattern$CharProperty.<init>(Pattern.java:3332)
>at java.util.regex.Pattern$BmpCharProperty.<init>(Pattern.java:3363)
>at java.util.regex.Pattern$BmpCharProperty.<init>(Pattern.java:3363)
>at java.util.regex.Pattern$Single.<init>(Pattern.java:3391)
>at java.util.regex.Pattern.newSingle(Pattern.java:2951)
>at java.util.regex.Pattern.atom(Pattern.java:1985)
>at java.util.regex.Pattern.sequence(Pattern.java:1885)
>at java.util.regex.Pattern.expr(Pattern.java:1752)
>at java.util.regex.Pattern.compile(Pattern.java:1460)
>at java.util.regex.Pattern.<init>(Pattern.java:1133)
>at java.util.regex.Pattern.compile(Pattern.java:823)
>at java.lang.String.split(String.java:2292)
#1誤差循環或條件,程序無法爲堆棧分配更多內存。 – Srinivas
嘗試調試器。或者編寫小型測試以實現更小的功能在當前形式中,問題是「太本地化」 – Jayan
異常的堆棧跟蹤是什麼? –