Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 索引越界异常,但可以';我不知道出了什么问题_Java_Arrays_Recursion_Arraylist - Fatal编程技术网

Java 索引越界异常,但可以';我不知道出了什么问题

Java 索引越界异常,但可以';我不知道出了什么问题,java,arrays,recursion,arraylist,Java,Arrays,Recursion,Arraylist,基本上,我必须接受用户输入,直到他们输入0,然后在数组中找到最大值、负数和正数之和。问题是我必须使用and array,而不能使用ArrayList调用其他方法,所以我想我应该创建一个ArrayList,并使用其中的元素来创建一个数组,因为数组中有未指定数量的元素。我一直认为这是错误的,并尝试了我能想到的一切。请帮忙 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at j

基本上,我必须接受用户输入,直到他们输入0,然后在数组中找到最大值、负数和正数之和。问题是我必须使用and array,而不能使用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)

//类说明:接受用户输入并将其放入数组中,直到输入0为止。
//然后计算最大值、负值和正值之和。
导入java.util.*;
导入java.text.*;;
公共课堂作业9{
//main方法初始化变量,然后调用方法赋值9
公共静态void main(字符串[]args)
{
整数计数=0;
ArrayList帮助=新建ArrayList();
任务9(帮助、计数);
}//端干管
//将用户输入添加到数组中,直到输入0;然后它调用其他方法
公共静态无效分配9(ArrayList帮助,整数计数)
{
DecimalFormat fmt=新的DecimalFormat(“#.##”);
双输入;
双最大值=0;
int负=0;
双和=0;
扫描仪扫描=新扫描仪(System.in);
输入=scan.nextInt();
如果(输入==0)
{
double[]number=新的double[help.size()];
for(int i=0;i最大值){
最大值=数字[计数];
计数--;
findMax(数字、计数、最大值);
}//如果结束
返回最大值;
}//末端findMax
公共静态int countNegative(双[]数字,int count,int负数)
{
如果(计数==-1)
{
返回负值;
}else if(数字[计数]<0){
负++;
计数--;
countNegative(数字、计数、负数);
}
返回负值;
}//结束计数负
public static double computeSumPositive(双[]个数字,整数计数,双和)
{
如果(计数==-1)
{
回报金额;
}否则{
总和=总和+数字[计数];
计数++;
computeSumPositive(数字、计数、总和);
回报金额;
}
}//结束计算正

}//end class

问题在于,您正在将数字
计数
传递到
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;
}
此版本在内部处理
计数
索引上限:

max = findMax(numbers, count, 0);

问题是您正在将数字
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;
}
此版本在内部处理
计数
索引上限:

max = findMax(numbers, count, 0);

异常给您一个提示:索引:3,大小:3。您的代码试图转到someArray[3],尽管它只有[0,1,2]。(数组在Java中是基于0的)我尝试将其更改为double[]numbers=new double[count],还尝试将其保留,并将for循环的参数更改为一系列不同的内容,但我不断收到错误,例如IndexOutOfBoundsException 3。有谁能帮我一个快速修复吗?现在我才意识到我认为是递归搞砸了,但idk出了什么问题异常给了你一个提示:索引:3,大小:3。您的代码试图转到someArray[3],尽管它只有[0,1,2]。(数组在Java中是基于0的)我尝试将其更改为double[]numbers=new double[count],还尝试将其保留,并将for循环的参数更改为一系列不同的内容,但我不断收到错误,例如IndexOutOfBoundsException 3。有人能帮我快速修复吗?好吧,现在我才意识到我认为是递归搞砸了,但idk出了什么问题,好吧,谢谢,我修复了所有这些错误,现在我的问题是打印最大值和总和的0。我假设返回答案并使用赋值方法打印是个问题。当您返回一个数字时,是否只有main方法可以访问它?如果是这样,那就意味着我必须调用主方法中的所有方法,对吗?你是说
assignment9
方法?Java是按值传递的,因此
max
值使得传入的
max
值中的任何更改都不会反映在方法
assignment9
中的
max
中。i、 e.使用返回的值对不起,我不太明白