如何确定 java.lang.ArrayIndexOutOfBoundsException 原因?

发布于 2024-12-11 18:08:12 字数 1779 浏览 0 评论 0原文

这是以下内容的延续: 递归斐波那契记忆

我不断得到 尝试运行代码时出现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-12-18 18:08:12

该数组的大小为 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 is num-1. You try to access index num (dictionary[num] = fib;) which is out of bounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文