简体   繁体   English

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:

So my assignment is write a recursive Java method that finds the maximum of an array of integers without using any loops. 因此,我的工作是编写一个递归Java方法,该方法无需使用任何循环即可查找整数数组的最大值。 The input is a first line that contains a single integer n < 10. The next line contains n numbers separated by spaces. 输入是第一行,其中包含一个整数n <10。下一行包含n个由空格分隔的数字。 The output should be a single integer. 输出应为单个整数。 Call your program FindMax. 调用您的程序FindMax。 The below code is what I have so far, it compiles but instead of me being able to enter 到目前为止,下面的代码是我可以编译的,但是我无法输入

input line 1: 5 (n) } 

This is what I need to be able to input 这是我需要能够输入的

input line2: 2 3 4 5 3 }

it makes me enter 它让我进入

input: 5 (n)
input: 2
input: 3
input: 4
input: 5
input: 3

also after entering the above I get: 同样在输入以上内容后,我得到:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at FindMaxtesting.getmax(FindMaxtesting.java:35)
at FindMaxtesting.getmax(FindMaxtesting.java:48)
at FindMaxtesting.getmax(FindMaxtesting.java:42)
at FindMaxtesting.getmax(FindMaxtesting.java:42)
at FindMaxtesting.getmax(FindMaxtesting.java:42)
at FindMaxtesting.getmax(FindMaxtesting.java:42)
at FindMaxtesting.main(FindMaxtesting.java:17)

This is my code so far: 到目前为止,这是我的代码:

import java.util.Scanner;

public class FindMaxtesting
{
   public static void main (String[]args)
   {
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt(); 
  int i = 0;
  int fin = 0;
  System.out.println(getmax( Inca(n ) , n , fin , i));
   }

   public static int[] Inca(int n )
   {
  Scanner sc = new Scanner(System.in);
  int[] arr = new int[n];
  for(int j=0;j<n;++j)
  {
     arr[j] = sc.nextInt();
      }
      return arr;   
   }    

   public static int getmax ( int arr[], int n, int fin, int i )
   {
      int temp = 0;
      if (fin < arr[i])
      {
         temp = fin;
     fin = arr[i];
     arr[i] = temp;
     i++;
     getmax(arr , n , fin , i);
      }

      else if (fin > arr[i])
      {
         i++;
     getmax(arr , n , fin , i);
      }

      else if ( i == n-1 )
      {
         return fin;
      }
      return fin;
   }
}

The essence of your problem is that in getmax you need to add the lines 问题的实质是在getmax中,您需要添加以下行

if (i >= n)
    return fin;

You are searching through the array recursively, which isn't really the best way to do it, but I assume that this is a homework problem, and you need to say when to stop looking further through the array, however you continue to look further in the array. 您正在递归地搜索数组,但这并不是最好的方法,但是我认为这是一个作业问题,您需要说什么时候停止在数组中进一步查找,但是您仍在继续查找在数组中。 When i>=n then you know you have searched the entire array, and can just return fin, as there are no further values that can be greater. 当i> = n时,您知道已经搜索了整个数组,并且可以返回fin,因为没有其他值可以更大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:8 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8 线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM