“线程中的异常”;“主要”;查找Max元素时出现java.lang.ArrayIndexOutOfBoundsException

“线程中的异常”;“主要”;查找Max元素时出现java.lang.ArrayIndexOutOfBoundsException,java,exception-handling,indexoutofboundsexception,Java,Exception Handling,Indexoutofboundsexception,每当我试图运行这段代码时,它都会给我一个越界异常。有人能告诉我出了什么问题吗 package com.programs.interview; import java.util.Scanner; public class FindMaxNumInArray { public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.print("Enter t

每当我试图运行这段代码时,它都会给我一个越界异常。有人能告诉我出了什么问题吗

package com.programs.interview;
import java.util.Scanner;

public class FindMaxNumInArray {

public static void main (String[] args) 
{ 
    Scanner scan = new Scanner (System.in); 
    System.out.print("Enter the size of the array: "); 
    int arraySize = scan.nextInt(); 
    int[] myArray = new int[arraySize]; 
    System.out.print("Enter the " + arraySize + " values of the array: "); 
    for (int i = 0; i < arraySize; i++)
        myArray[i] = scan.nextInt(); 
    for (int j = 0; j < arraySize; j++) 
        System.out.println(myArray[j]); 
    System.out.println("In the array entered, the larget value is "+ maximum(myArray,arraySize) + "."); 
} 

public static int maximum(int[] arr, int Arraylength){
    int tmp;
    if (Arraylength == 0)
        return arr[Arraylength];
    tmp = maximum(arr, Arraylength -1);
    if (arr[Arraylength] > tmp)
        return arr[Arraylength];
    return tmp;
  }
}
package.com.programs.interview;
导入java.util.Scanner;
公共类FindMaxNumInArray{
公共静态void main(字符串[]args)
{ 
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入数组的大小:”);
int arraySize=scan.nextInt();
int[]myArray=新int[arraySize];
System.out.print(“输入数组的“+arraySize+”值:”);
for(int i=0;itmp)
返回arr[阵列长度];
返回tmp;
}
}
输出

输入数组的大小:5输入数组的5个值:1 2 3 线程“main”中的4 5 1 2 3 4 5异常 java.lang.ArrayIndexOutOfBoundsException:5 at max(FindMaxNumInArray.java:26) 在 com.programs.interview.FindMaxNumInArray.main(FindMaxNumInArray.java:17)

这就是问题所在:

if (arr[Arraylength] > tmp)
有效的数组索引从
0
length-1
不等
array[array.length]
始终无效,在初始调用时,
ArrayLength
等于
arr.length

老实说,不清楚为什么要使用递归。迭代解决方案要简单得多,但如果数组为空,则需要确定要执行的操作

编辑:如果您真的想知道我将如何编写递归表单,应该是这样的:

/** Returns the maximum value in the array. */
private static int maximum(int[] array) {
    if (array.length == 0) {
        // You need to decide what to do here... throw an exception,
        // return some constant, whatever.
    }
    // Okay, so the length will definitely be at least 1...
    return maximumRecursive(array, array.length);
}

/** Returns the maximum value in array in the range [0, upperBoundExclusive) */
private static int maximumRecursive(int[] array, int upperBoundExclusive) {
    // We know that upperBoundExclusive cannot be lower than 1, due to the
    // way that this is called. You could add a precondition if you really
    // wanted.
    if (upperBoundExclusive == 1) {
        return array[0];
    }
    int earlierMax = maximumRecursive(array, upperBoundExclusive - 1);
    int topValue = array[upperBoundExclusive - 1];
    return Math.max(topValue, earlierMax);

    // Or if you don't want to use Math.max
    // return earlierMax > topValue ? earlierMax : topValue;
}
这就是问题所在:

if (arr[Arraylength] > tmp)
有效的数组索引从
0
length-1
不等
array[array.length]
始终无效,在初始调用时,
ArrayLength
等于
arr.length

老实说,不清楚为什么要使用递归。迭代解决方案要简单得多,但如果数组为空,则需要确定要执行的操作

编辑:如果您真的想知道我将如何编写递归表单,应该是这样的:

/** Returns the maximum value in the array. */
private static int maximum(int[] array) {
    if (array.length == 0) {
        // You need to decide what to do here... throw an exception,
        // return some constant, whatever.
    }
    // Okay, so the length will definitely be at least 1...
    return maximumRecursive(array, array.length);
}

/** Returns the maximum value in array in the range [0, upperBoundExclusive) */
private static int maximumRecursive(int[] array, int upperBoundExclusive) {
    // We know that upperBoundExclusive cannot be lower than 1, due to the
    // way that this is called. You could add a precondition if you really
    // wanted.
    if (upperBoundExclusive == 1) {
        return array[0];
    }
    int earlierMax = maximumRecursive(array, upperBoundExclusive - 1);
    int topValue = array[upperBoundExclusive - 1];
    return Math.max(topValue, earlierMax);

    // Or if you don't want to use Math.max
    // return earlierMax > topValue ? earlierMax : topValue;
}
您无法访问

arr[Arraylength]
最后一个元素是

arr[Arraylength -1]
例如,如果你有

int arr[] = new int[5];
然后元素将位于
4
,因为索引从
0

arr[0], arr[1], arr[2], arr[3], arr[4]
您无法访问

arr[Arraylength]
最后一个元素是

arr[Arraylength -1]
例如,如果你有

int arr[] = new int[5];
然后元素将位于
4
,因为索引从
0

arr[0], arr[1], arr[2], arr[3], arr[4]

您的问题出现在以下代码中:

if (arr[Arraylength] > tmp)
            return arr[Arraylength];

索引从0开始,因此对于包含5个元素的数组,您将超出范围
[1,2,3,4,5]
索引:
[0,1,2,3,4]
您的问题在以下代码段中:

if (arr[Arraylength] > tmp)
            return arr[Arraylength];

索引从0开始,因此对于包含5个元素的数组,您将超出范围
[1,2,3,4,5]
索引:
[0,1,2,3,4]
我将使用普通循环。Java并不擅长递归

public static int maximum(int[] arr) {
    int max = Integer.MIN_VALUE;
    for(int i : arr) if (i > max) max = i;
    return max;
}

我会使用一个普通的循环。Java并不擅长递归

public static int maximum(int[] arr) {
    int max = Integer.MIN_VALUE;
    for(int i : arr) if (i > max) max = i;
    return max;
}
这里

您正在传递arraysize,其中在max方法中返回arr[arraylelength],该方法给出ArrayIndexOutOfBound,因此在调用max(yArray,arraysize-1)返回arr[arraylelength-1]语句时进行更改。


您正在传递arraysize,其中在maximum方法中返回arr[Arraylength],该方法给出ArrayIndexOutOfBound,因此在调用maximum(yArray,arraysize-1)返回arr[Arraylength-1]时进行更改语句。

控制台给了我以下输出:输入数组的大小:5输入数组的5个值:1 2 3 4 5 1 2 3 4 5线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:5在com.programs.interview.FindMaxNumInArray.maximum(FindMaxNumInArray.java:26)在com.programs.interview.FindMaxNumInArray.main(FindMaxNumInArray.java:17)上可以,因为包含5个元素的数组没有元素索引[5]。只有0 1 2 3 4它必须是递归的吗?使用循环会更简单、更快。那么。。。?我的意思是用5初始化数组有问题吗?实际上是编程中的新手,我接受了递归方法的采访。控制台给了我这个输出:输入数组的大小:5输入数组的5个值:1 2 3 4 5 1 2 3 4 5线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:5位于com.programs.accept.FindMaxNumInArray.maximum(FindMaxNumInArray.java:26)位于com.programs.accept.FindMaxNumInArray.main(FindMaxNumInArray.java:17)是,因为具有5个元素的数组没有元素索引[5]。只有0 1 2 3 4它必须是递归的吗?使用循环会更简单、更快。那么。。。?我的意思是用5?m初始化数组有问题吗?实际上是编程中的新手,我接受了递归方法的采访。你的意思是这会有帮助:公共静态int-max(int[]arr,int-arrayllength){int-tmp;if(arrayllength==0)返回arr[arrayllength];tmp=max(arr,arrayllength-1);if(arr[Arraylength-1]>tmp)返回arr[Arraylength];返回tmp;}@AliAnsari:不,这仍然会失败-您仍然返回
arr[Arraylength]
,如果数组为空,那么您仍然会尝试从中获取第一个元素。同样,您确实需要这样做