[英]java stack overflow error recursion
我正在制作一個方法,使用一個參數來左移和右移數組中的字符,該參數指示要移位多少次。 它必須在20毫秒內完成,所以我嘗試了遞歸。
//在數組中切換位置的方法
public static void bytt(char[] c, int i, int j){
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
//這個方法向左移動
public static char rotasjon1(char[] a, int i){
if(i > 0){
bytt(a,i,i-1);
return rotasjon1(a,i-1);
}
else
return ' ';
}
//這個方法向右移
public static char reverseRotasjon(char[] a, int i){
if(i < a.length-1){
bytt(a,i,i+1);
return reverseRotasjon(a,i+1);
}
else
return ' ';
}
//此方法根據參數決定使用右移或左移
public static void rotasjon(final char[] a, int k){
if(a.length == 1 || a.length == 0){
return;
}
if(k >= 0){
for(int i = 0; i< k; i++){
char temp = a[a.length-1];
rotasjon1(a,a.length-1);
a[0] = temp;
}
}
if(k < 0){
for(int i = k; i< 0; i++) {
char temp = a[0];
reverseRotasjon(a, 0);
a[a.length - 1] = temp;
}
}
}
//所有這些都適用於這個數組
char[] d = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
char[] d0 = {'G', 'H', 'I', 'J', 'A', 'B', 'C', 'D', 'E', 'F'};
Oblig1.rotasjon(d, 4);
d = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
Oblig1.rotasjon(d, -6);
//但是我得到了帶有這個數組的java.lang.StackOverflowError
char[] x = new char[100_000];
Oblig1.rotasjon(x, 99_999);
我知道數組很重要,但有可能解決這個問題,還是我必須回到傳統的for循環? 它必須在20毫秒內執行
我知道數組很重要,但有可能解決這個問題,還是我必須回到傳統的for循環?
發生異常是因為遞歸太深; 即它需要太多嵌套調用。
現在有多種語言,這無關緊要。 例如,使用典型的函數語言,您可以根據需要進行深度遞歸。 但有效的原因是函數式語言(以及許多其他語言)實現了一種稱為尾部調用優化的東西,其中在方法調用結束時的遞歸調用被優化(由編譯器)到跳轉到開始的方法。
參考: 什么是尾部調用優化?
但Java不支持尾調用優化。 (有一些合理但復雜的原因。)相反,每次調用都會在當前線程的堆棧上獲得一個堆棧幀; 即N深遞歸需要N個堆棧幀。 問題是Java線程具有固定數量的堆棧空間。 (默認值通常為1M字節或更少。)創建后,無法擴展線程的堆棧。 如果算法過於冗長,則線程會耗盡堆棧空間,並且JVM會引發異常...正如您所觀察到的那樣。
所以答案是什么?
通常,避免在Java中實現可能深度遞歸的算法:
如果算法是遞歸的,請嘗試將其轉換為迭代等價物; 例如,手動進行尾調用優化。
如果算法是迭代的,那就這樣吧!
如果確實需要深度遞歸,則可以將線程的maxiumum堆棧大小指定為構造函數參數。 (我不確定是否存在架構限制,但您肯定會受限於可用的內存量......)
如果是的話,mabye你有一些建議嗎? 記住它必須在20毫秒內執行。
如果您的主要目標是有效地實現此目的,請不要使用遞歸而不是迭代。 在Java中 - 它不會更快,並且總是存在堆棧溢出的潛在風險。
在這種情況下,請查看使用臨時數組和System.arraycopy
。 (如果您旋轉1,則不需要臨時數組。您可以一次以1為步長旋轉N
,但這樣效率很低。)
在這種情況下,看看實現它,因為您將手動重新排列撲克牌...只使用兩只手(臨時變量)。 這給出了“旋轉N
”問題的解決方案,而不使用O(N)
額外存儲。
使用System.arraycopy
建議的System.arraycopy
超快速旋轉:
private static void rotate(char[] array, int distance) {
if (array == null || array.length == 0)
return; // nothing to rotate
final int len = array.length;
int d = distance % len; // eliminate distance overflow, e.g. for len=10, shift +28 is same as +8
if (d == 0)
return; // not rotating
if (d < 0)
d += len; // convert left shift to right shift, e.g. for len=10, -2 is same as +8
if (d < len / 2) { // right shift less than half the array
char[] temp = new char[d];
System.arraycopy(array, len - d, temp, 0, d); // save d values at end
System.arraycopy(array, 0, array, d, len - d); // shift right by d
System.arraycopy(temp, 0, array, 0, d); // add saved value at start
} else { // right shift more than half the array, so better to use left shift for smaller temp space
d = len - d; // e.g. for len=10, right by 8 is left by 2
char[] temp = new char[d];
System.arraycopy(array, 0, temp, 0, d); // save d values at start
System.arraycopy(array, d, array, 0, len - d); // shift left by d
System.arraycopy(temp, 0, array, len - d, d); // add saved value at end
}
}
測試
String s = "ABCDEFGHIJ";
for (int i = -11; i <= 11; i++) {
char[] array = s.toCharArray();
long start = System.nanoTime();
rotate(array, i);
long end = System.nanoTime();
System.out.printf("%3d: %s (%dns)%n", i, new String(array), end-start);
}
char[] x = new char[100_000];
for (int d : new int[] { 0, 1, 50_000, 99_999 }) {
long start = System.nanoTime();
rotate(x, d);
long end = System.nanoTime();
System.out.printf("%5d: %6dns = %fms%n", d, end-start, (end-start) / 1_000_000d);
}
產量
-11: BCDEFGHIJA (7128ns)
-10: ABCDEFGHIJ (285ns)
-9: JABCDEFGHI (856ns)
-8: IJABCDEFGH (855ns)
-7: HIJABCDEFG (855ns)
-6: GHIJABCDEF (855ns)
-5: FGHIJABCDE (855ns)
-4: EFGHIJABCD (855ns)
-3: DEFGHIJABC (856ns)
-2: CDEFGHIJAB (855ns)
-1: BCDEFGHIJA (855ns)
0: ABCDEFGHIJ (286ns)
1: JABCDEFGHI (855ns)
2: IJABCDEFGH (856ns)
3: HIJABCDEFG (1710ns)
4: GHIJABCDEF (856ns)
5: FGHIJABCDE (1141ns)
6: EFGHIJABCD (855ns)
7: DEFGHIJABC (856ns)
8: CDEFGHIJAB (855ns)
9: BCDEFGHIJA (571ns)
10: ABCDEFGHIJ (285ns)
11: JABCDEFGHI (855ns)
0: 285ns = 0.000285ms
1: 55885ns = 0.055885ms
50000: 43339ns = 0.043339ms
99999: 56169ns = 0.056169ms
Java不支持尾調用優化,因此您需要不同的算法或for循環。
您要做的是使用大小為100 000到1的輸入數組調用該函數100 000次。您嘗試裝入內存的最大大小為(100 000 + 1)*(100 000/2) * 8(所有字符的大小)+ 100 000 * 8(數組引用的大小)+ 100 000 * 24(保持數組結構所需的內存)位。 這是5 000 450 000字節或~5GB,因此是StackOverflowError。 您仍然可以通過調整JVM來使其運行,但一般來說,對大型數據結構使用深度遞歸是一個非常糟糕的主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.