【问题标题】:Index out of bounds Exception but can't figure out what is wrong索引超出范围异常但无法找出问题所在
【发布时间】:2013-03-29 21:05:44
【问题描述】:

基本上,我必须接受用户输入,直到他们输入 0,然后找到数组中的最大值、负数的数量和正数的总和。问题是我必须使用和数组并且不能使用 ArrayList 来调用其他方法,所以我想我会创建一个 ArrayList 并使用它的元素来创建一个数组,因为数组中有未指定数量的元素。我一直把它当作错误,并尝试了我能想到的一切。请帮忙。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at Assignment9.assignment9(Assignment9.java:37)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.main(Assignment9.java:17)

//Class Description: Takes user input and makes it into an array until 0 is entered.
// It then computes the maximum #, amount of negative #s and the sum of the positive #s.
import java.util.*;
import java.text.*;;

public class Assignment9 {

//main method initializes variables then calls method assignment9
public static void main(String[] args)
{
    int count = 0;
    ArrayList<Double> help = new ArrayList<Double>();
    assignment9(help, count);
}//end main

//adds user input to array until 0 is entered; it then calls other methods
public static void assignment9(ArrayList<Double> help, int count)
{
    DecimalFormat fmt = new DecimalFormat("#.##");
    double input;
    double max = 0;
    int negative = 0;
    double sum = 0;
    Scanner scan = new Scanner(System.in);

    input = scan.nextInt();

    if (input == 0)
    {
        double[] numbers = new double[help.size()];
        for(int i = 0; i < numbers.length; i++)
        {
            numbers[i] = help.get(i);
        }
        findMax(numbers, count, max);
        countNegative(numbers, count, negative);
        computeSumPositive(numbers, count, sum);
        System.out.println("The maximum numer is " + fmt.format(max));
        System.out.println("The total number of negative numbers is " + negative);
        System.out.println("The sum of positive numbers is " + fmt.format(sum));
        System.exit(0);
    }else{
            help.add(input);
            count++;
            assignment9(help, count);
    }//end if
}//end assignment9

//compares elements of array to find the max until count = -1
public static double findMax(double[] numbers, int count, double max)
{

    if (count == -1)
    {
        return max;
    }else if(numbers[count] > max){
        max = numbers[count];
        count--;
        findMax(numbers, count, max);
    }//end if
    return max;

}//end findMax

public static int countNegative(double[] numbers, int count, int negative)
{

    if(count == -1)
    {
        return negative;
    }else if(numbers[count] < 0){
        negative++;
        count--;
        countNegative(numbers, count, negative);
    }
    return negative;
}//end countNegative

public static double computeSumPositive(double[] numbers, int count, double sum)
{
     if(count == -1)
     {
         return sum;
     }else{
         sum = sum + numbers[count];
         count++;
         computeSumPositive(numbers, count, sum);
         return sum;
     }
}//end computeSumPositive

}//结束类

【问题讨论】:

  • 异常给你提示:Index: 3, Size: 3。您的代码正在尝试转到 someArray[3],尽管它只有 [0,1,2]。 (数组在 Java 中是 0)
  • 我尝试将其更改为 double[]numbers = new double[count] 并尝试离开它并将 for 循环的参数更改为一堆不同的东西,但我不断收到诸如 IndexOutOfBoundsException 之类的错误3. 谁能帮我快速解决?
  • 好吧,现在我才意识到我认为它的递归搞砸了,但我知道出了什么问题

标签: java arrays recursion arraylist


【解决方案1】:

问题是您将数字 count 传递给 findMax 并在尝试访问此行的上限时抛出 ArrayIndexOutOfBoundsException

} else if (numbers[count] > max) {

请记住,Java 中的数组是从零开始的。此外,由于 Java 使用 按值传递,您需要将结果分配给 findMax 的输出:

max = findMax(numbers, count - 1, 0);

从逻辑上讲,findMax 将始终返回最后一个的数字,而不是最大的数字。您需要从递归方法中返回临时的max

double findMax(double numbers[], int count, int index) {
   // This needs to be size - 1 because the array is 0-indexed.
   // This is our base case
   if (index == count - 1)
      return numbers[index];

   // Call the function recursively on less of the array
   double result = findMax(numbers, count, index + 1);

   // Return the max of (the first element we are examining, the max of the
   // rest of the array)
   if (numbers[index] > result)
      return numbers[index];
   else
      return result;
}

此版本内部处理count 索引上限:

max = findMax(numbers, count, 0);

【讨论】:

  • 好的,谢谢,我修复了所有这些错误,现在我的问题是它正在为最大值和总和打印 0。我假设返回答案并使用分配方法打印它们是一个问题。当您返回一个数字时,是否只有 main 方法可以访问它?如果是这样,那意味着我必须调用我的主要方法中的所有方法吗?
  • 您的意思是assignment9 方法? Java 是按值传递,所以max 值因此传入的max 值的任何更改都不会反映在方法assignment9 中的max 中。即使用返回值
  • 对不起,我不太明白。您的意思是可以在 assignment9 方法中使用重新调整的值吗?如果可以,为什么打印 0?还是我必须使用 main 方法来打印?
  • 可以使用assignment9
猜你喜欢
  • 2018-03-02
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2023-04-10
相关资源
最近更新 更多