English 中文(简体)
• 如何减少 j。
原标题:How to nail down the java.lang.ArrayIndexOutOfBoundsException cause?

This is a continuation from: Recursive Fibonacci memoization.

I keep getting an java.lang.ArrayIndexOutOfBoundsException error when trying to run the code. I m getting the error on lines 63 and 40 which are

63: int fib = dictionary[num]; // dictionary is a class level array.
40: answer = fibonacci(num); // Answer is an int calling the fibonacci function and passing "num" to it.

这里的全文是:

import javax.swing.JOptionPane;

public class question2
{

//class variable
static int count = 0;
static int [] dictionary;


//main method
public static void main(String[] args)
{

//user input
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter num:")), answer;

//Catches negative numbers, exits
if (num < 0) 
{
    JOptionPane.showMessageDialog(null,"ERROR: fibonacci sequence not defined for negative   numbers.");
    System.exit(1);
 }

//info dialog
JOptionPane.showMessageDialog(null,"About to calculate fibonacci("+num+")");

//giving the array "num" elements
dictionary = new int [num];

//fill array with 0
for (int i=0; i<dictionary.length;i++)
dictionary[i]=0;

//adds value of 1 for fib(1)    
if (dictionary.length>=2)
dictionary[1]= 1;

//method call
answer = fibonacci(num);

//output
JOptionPane.showMessageDialog(null,"Fibonacci("+num+") is "+answer+" (took "+count+" calls)");
}


  static int fibonacci(int num)
  {
count++;

// Base cases: f(0) is 0, f(1) is 1
if (num==0)
return 0;

if (num==1)
return 1;

// Other cases: f(num) = f(num-1) + f(num-2)/
else 
{

  //check array for value
  int fib = dictionary[num];

  //add new value to array
  if (fib==0) 
  {
    fib = fibonacci(num-1) + fibonacci(num-2);
    dictionary[num] = fib;
  }
  return fib;
}

  }


} //class terminator
最佳回答

阵列的大小为num(int fib = dictionary [num];,使您能够查阅的最高指数为num-1。 页: 1 不受约束。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签