查找元素最大值最小的数组

我有一个 int[] 数组的数组列表,我试图找到具有最低最大元素的数组。


例如,对于数组 [1,2,5]、[0,1,2]、[8,0,0],它将是数组 [0,1,2],因为最大元素为 2。


但是,我的代码无法正常工作。你能帮我修复我的代码吗?谢谢!


int min = Integer.MAX_VALUE;

for (int i=0; i<list.size(); i++) {

    for (int j=0; j<list.get(i).length; j++) {

        if (list.get(i)[j]<min) {

            min = list.get(i)[i]; 

            minIndex = i; //index of the array in arraylist

        }

    }

}


慕姐8265434
浏览 85回答 3
3回答

素胚勾勒不出你

您的代码在所有数组中找到最低值(至少如果您修复 to 的拼写错误,它会list.get(i)[i])list.get(i)[j]。您应该找到每个数组的最大元素,然后检查该最大值是否小于所有先前找到的最大值:int minIndex = 0;int min = Integer.MAX_VALUE;for (int i=0; i<list.size(); i++) {&nbsp; &nbsp; int max = Integer.MIN_VALUE;&nbsp; &nbsp; for (int j=0; j<list.get(i).length; j++) { // find max element of current array&nbsp; &nbsp; &nbsp; &nbsp; if (list.get(i)[j] > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = list.get(i)[j];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (max > min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; // we already know the max of this array is not the smallest max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (max < min) { // check if the max of the current array is the smallest max so far&nbsp; &nbsp; &nbsp; &nbsp; min = max;&nbsp; &nbsp; &nbsp; &nbsp; minIndex = i; //index of the array in arraylist&nbsp; &nbsp; }}

噜噜哒

你可以试试这个,例如:import java.util.Arrays;import java.util.Comparator;public class MaxMinArray {&nbsp; &nbsp; public static int[]&nbsp; maxMinFromArray(int[][] arrays){&nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; Arrays.stream(arrays).min(Comparator.comparingInt(x -> Arrays.stream(x).max().orElse(0))).orElse(null);&nbsp; &nbsp; }}我已经用你的例子测试过了 :) :import org.junit.Test;import static org.junit.Assert.*;public class MaxMinArrayTest {&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testMaxMinArray(){&nbsp; &nbsp; &nbsp; &nbsp; int[][] arrays = new int[][] {{1,2,5}, {0,1,2}, {8,0,0}};&nbsp; &nbsp; &nbsp; &nbsp; int[] result = MaxMinArray.maxMinFromArray(arrays);&nbsp; &nbsp; &nbsp; &nbsp; assertArrayEquals(new int[]{0,1,2}, result);&nbsp; &nbsp; }}

青春有我

作为 for 循环的替代方法,您可以尝试使用stream api来解决此问题。这个想法是完全一样的:在每个子列表中找到最大元素。使用Comparator在最大元素中查找具有最小元素的子列表。List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .min((a, b) ->&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.stream().max(Integer::compare).get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.compareTo(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.stream().max(Integer::compare).get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; ).get();&nbsp;代码更少,可以说很容易理解代码的意图。您甚至可以使用以下方法缩短代码Comparator::comparing:List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .min(Comparator.comparing(Collections::max))&nbsp; &nbsp; &nbsp; &nbsp; .get();让我们更详细地看看这里发生了什么。List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))&nbsp; &nbsp; &nbsp; &nbsp; // lets get stream of list Stream<List<Integer>>.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; // find sublist which has minimum maximum element.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .min((a, b) ->&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// a & b are sublist, for example: [1,2,5], [0,1,2]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// find maximum from a [1,2,5] which is [5]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.stream().max(Integer::compare).get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// compare maximum from a to maximum from b&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.compareTo(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// find maximum from a [0,1,2] which is [2]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.stream().max(Integer::compare).get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// get minimum out of [5,2]&nbsp; &nbsp; &nbsp; &nbsp; ).get(); // [0, 1, 2]所以执行可能看起来类似于这样:Initial list is: [1,2,5], [0,1,2], [8, 0, 0]find minimum list based on maximum:&nbsp;min( max([1,2,5]), max([0,1,2]))&nbsp;min( [5], [2])&nbsp;[2] -> list [0,1,2] contains minimum maximum element so far, go the the next iterationfind minimum list based on maximum:&nbsp;min( max([0,1,2]), max([8, 0, 0]) )&nbsp;min( [2], [8])&nbsp;[2] -> list [0,1,2] contains minimum maximum element so far,&nbsp;there no other element in the stream, [0,1,2] is final result.&nbsp;希望这个对你有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java