2011-11-27 12 views
0

當我運行下面的程序我收到以下錯誤:(?任何想法)BinaryRecursion錯誤消息:的StackOverflowError

Exception in thread "main" java.lang.StackOverflowError 
at SumArray.binarySum(SumArray.java:31) 
at SumArray.binarySum(SumArray.java:34) 

下面是代碼:我需要生成隨機整數數組與使用二進制和線性遞歸增加其值...

import java.io.*; 
import java.util.ArrayList; 
import java.util.Random; 

class SumArray { 
    static int floor = 100000; 
    static int ceil = 0; 
    int result; 
    int half; 
    int half2; 

    //Linear Recursion 
    int sum(int a[], int n) { 
     if (n == 1) 
      return a[0]; 
     else { 
      result = sum(a, n - 1); 
      result = result + a[n - 1]; 
      return result; 
     } 
    } 

    //Binary Recursion 
    int binarySum(int a[], int i, int n) { 
     if (n == 1) 
      return a[0]; 

     return binarySum(a, i, ceil/2) + binarySum(a, i + ceil/2, floor/2); 
    } 

    public static void main(String args[]) throws IOException { 

     //Define ArrayList to hold Integer objects 
     ArrayList<Integer> numbers = new ArrayList<Integer>(); 

     // User determines number of elements 
     DataInputStream dis = new DataInputStream(System.in); 
     System.out.println("Hello! Please enter the size of your array: "); 
     int n = Integer.parseInt(dis.readLine()); 

     // Generate specified number of random integers 
     Random randNumGenerator = new Random(); 
     int[] x = new int[n]; 

     // While still less than indicated number.. add more 
     for (int i = 0; i < x.length; i++) { 
      x[i] = (randNumGenerator.nextInt(100000)); 
      numbers.add(x[i]); 

      //calculate min and max values 
      if(ceil < x[i]) { 
       ceil = x[i]; 
      } 
      if(floor > x[i]) { 
       floor = x[i]; 
      } 
     } 

     SumArray a1 = new SumArray(); 

     // Print array values 
     System.out.println("Array values inlude: " + numbers); 

     // Print the sum 
     System.out.println("The sum of the array using Linear Recursion is: " + a1.sum(x, n)); 

     // Print the binary sum 
     System.out.println("The sum of the array using Binary Recursion is: " + a1.binarySum(x, n, n)); 

     System.out.println("Ceiling: " + ceil); 
     System.out.println("Floor: " + floor); 
    } 
} 
+0

提示:在一些診斷中顯示每個遞歸調用的參數。然後想想'binarySum(什麼,什麼,0)'會做什麼...... –

回答

2

你遞歸地調用你的BinarySum方法,它的第三個參數是ceil/2,而floor/2永遠不會改變。 ceil/2是0,floor/2是50000,這兩個都不是1,所以你得到了無限遞歸。似乎你可能想這些值是不同的每個遞歸調用...

我的Java有點生疏(也許有人更習慣於在Java編碼可以清理這個了),但作爲一個例子,嘗試類似這樣的:

class Accumulator { 
    static int binarySum(int a[]) { 
    return binarySum(a, 0, a.length-1); 
    } 

    static int binarySum(int a[], int s, int e) { 
    if (e-s == 0) return a[s]; 
    int mid = s + ((e-s)/2); 
    return binarySum(a, s, mid) + binarySum(a, mid+1, e); 
    } 

    public static void main(String args[]) { 
    int[] vals = {2,3,4,5,6,7}; 
    System.out.println(binarySum(vals)); 
    } 
} 

每次調用我們分裂我們所搜索成兩半和遞歸,直到我們得到到一個單一的項目。

+0

這是完美的,謝謝! – javaNewb37

1

在二進制和,如果n == 0,你會繼續無限遞歸,並最終耗盡堆棧空間。嘗試修復,看看它是否有幫助。祝你好運!

0

binarySum獲取參數n,該參數不在函數內部使用。因此,遞歸不會將工作分成兩部分,因此永遠不會結束。

此外:您正在混合陣列索引(ni)與內容的陣列的ceilfloor)。這也是相當簡單的錯誤。

提示:在binarySum的開頭插入一個System.out.println,打印出所有相關的變量。你會很快看到什麼是錯的。