Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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_Stack Overflow - Fatal编程技术网

Java 在数组中查找最小值和最大值时发生堆栈溢出错误?

Java 在数组中查找最小值和最大值时发生堆栈溢出错误?,java,arrays,stack-overflow,Java,Arrays,Stack Overflow,我正在解决一个问题,以找到数组中的最小值和最大值。我有下面的程序,每当我运行它时,我都会看到java.lang.StackOverflowerError: public class MinMaxInArray { public static void main(String[] args) { int a1[] = { 3, 4, 2, 6, 8, 1, 9, 12, 15, 11 }; Pair result = getMinMax(a1, 0, a1

我正在解决一个问题,以找到数组中的最小值和最大值。我有下面的程序,每当我运行它时,我都会看到
java.lang.StackOverflowerError

public class MinMaxInArray {

    public static void main(String[] args) {
        int a1[] = { 3, 4, 2, 6, 8, 1, 9, 12, 15, 11 };
        Pair result = getMinMax(a1, 0, a1.length - 1);

        System.out.println("Min: " + result.min);
        System.out.println("Max: " + result.max);
    }

    public static Pair getMinMax(int[] arr, int low, int high) {
        Pair result = new Pair();
        Pair left = new Pair();
        Pair right = new Pair();

        // if there is only one element arr= {1}
        if (low == high) {
            result.min = arr[low];
            result.max = arr[high];
        }

        // if there are two element arr={1,2}
        if (high == low + 1) {
            if (arr[low] > arr[high]) {
                result.max = arr[low];
                result.min = arr[high];
            } else {
                result.max = arr[high];
                result.min = arr[low];
            }
            return result;
        }
        // if there are more than 2 elements
        int mid = (low + high) / 2;
        left = getMinMax(arr, low, mid);
        right = getMinMax(arr, mid + 1, high);

        if (left.min < right.min) {
            result.min = left.min;
        } else {
            result.min = right.min;
        }
        if (left.max > right.max) {
            result.max = left.max;
        } else {
            result.max = right.max;
        }
        return result;

    }

    static class Pair {
        int min;
        int max;
    }
}
public类MinMaxInArray{
公共静态void main(字符串[]args){
INTA1[]={3,4,2,6,8,1,9,12,15,11};
配对结果=getMinMax(a1,0,a1.length-1);
System.out.println(“Min:+result.Min”);
System.out.println(“Max:+result.Max”);
}
公共静态对getMinMax(int[]arr,int低,int高){
配对结果=新配对();
左对=新对();
右对=新对();
//如果只有一个元素arr={1}
如果(低==高){
result.min=arr[低];
result.max=arr[高];
}
//如果有两个元素arr={1,2}
如果(高==低+1){
如果(arr[低]>arr[高]){
result.max=arr[低];
result.min=arr[高];
}否则{
result.max=arr[高];
result.min=arr[低];
}
返回结果;
}
//如果有两个以上的元素
int mid=(低+高)/2;
左=getMinMax(arr、低、中);
右=getMinMax(arr,中+1,高);
如果(左.min<右.min){
result.min=left.min;
}否则{
result.min=right.min;
}
如果(left.max>right.max){
result.max=left.max;
}否则{
result.max=right.max;
}
返回结果;
}
静态类对{
int-min;
int max;
}
}

为什么会抛出此错误,它意味着什么?如何修复此问题?

您忘记了
返回结果在这段代码中:

// if there is only one element arr= {1}
    if (low == high) {
        result.min = arr[low];
        result.max = arr[high];
        return result;
    }

我认为递归对于这个任务没有意义。此代码运行良好:

    int a1[] = { 3, 4, 2, 6, 8, 1, 9, 12, 15, 11 };
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (int a : a1) {
        if (a < min) {
            min = a;
        }
        if (a > max) {
            max = a;
        }
    }
    System.out.println("Min: " + min);
    System.out.println("Max: " + max);
inta1[]={3,4,2,6,8,1,9,12,15,11};
int min=整数最大值;
int max=整数的最小值;
对于(INTA:a1){
如果(a最大值){
max=a;
}
}
System.out.println(“Min:+Min”);
System.out.println(“Max:+Max”);

除非您希望以递归方式执行,否则最简单的解决方案是使用流:

IntSummaryStatistics stats = Arrays.stream(a1).summaryStatistics();
int max = stats.getMax();
int min = stats.getMin();

// or:

int max = Arrays.stream(a1).max();
int min = Arrays.stream(a1).min();
另一种方法是使用for循环并简单地检查每个数字:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i : a1) {
    if (i > max) {
        max = i;
    }
    if (i < min) {
        min = i;
    }
}
int max=Integer.MIN\u值;
int min=整数最大值;
对于(int i:a1){
如果(i>max){
max=i;
}
如果(i
错误在哪里抛出(哪一行)?你为什么要这样做?通常这个错误是无限递归的结果。@Bubletan,看起来像是他的老师让他递归地做的…@Bubletan有更好的方法吗?我正在准备面试,所以我想这会更容易些。什么,就这样?我当时很笨。这在时间和空间上的复杂性是什么?主要的一点是没有比这更快的解决方案了。而且它占用尽可能少的空间