如何确定 java.lang.ArrayIndexOutOfBoundsException 原因?
这是以下内容的延续: 递归斐波那契记忆。
我不断得到 尝试运行代码时出现 java.lang.ArrayIndexOutOfBoundsException 错误。我在第 63 行和第 40 行收到错误,这是
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
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.
Here is the full code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该数组的大小为
num
(int fib =dictionary[num];
),因此您可以访问的最大索引为num-1
。您尝试访问超出范围的索引num
(dictionary[num] = fib;
)。The array is of size
num
(int fib = dictionary[num];
) so the max index you can access isnum-1
. You try to access indexnum
(dictionary[num] = fib;
) which is out of bounds.