java - 查找最大元素时线程 "main"java.lang.ArrayIndexOutOfBoundsException 中出现异常

标签 java exception 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 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;
  }
}

输出

Enter the size of the array: 5 Enter the 5 values of the array: 1 2 3 4 5 1 2 3 4 5 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at com.programs.interview.FindMaxNumInArray.maximum(FindMaxNumInArray.java:26) at com.programs.interview.FindMaxNumInArray.main(FindMaxNumInArray.java:17)

最佳答案

这就是问题:

if (arr[Arraylength] > tmp)

有效的数组索引从 0length-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;
}

关于java - 查找最大元素时线程 "main"java.lang.ArrayIndexOutOfBoundsException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21960861/

相关文章:

通过 Jenkins(使用 Maven)在 Tomcat7 上部署 war 时,java.net.socketexception 连接由对等套接字写入错误重置

java - 通过手绘撤消和移动功能

Hibernate 抛出 org.hibernate.persister.entity.SingleTableEntityPersister

java - 填充二维数组迷宫时数组越界异常

c - 无越界错误

java - 在 servlet-context 中的 dev 和 prod 值之间切换的标志

java - Spring 4.1.4 和 Jackson 2.5,注释驱动设置不将 REST 响应转换为 JSON

c# - 我应该为失败的文件解析抛出什么异常?

java - 如何摆脱线程中的异常 "main"java.lang.NoClassDefFoundError : com/codexus/resources/ResourcesManager?

java - 显示 StringIndexOutOfBoundExceptions 错误