Java 查找元素最大值最低的数组
我有一个int[]数组的arraylist,我试图找到max元素最低的数组 例如,从数组[1,2,5]、[0,1,2]、[8,0,0]中,它将是数组[0,1,2],因为max元素是2 但是,我的代码工作不正常。你能帮我修改代码吗?谢谢Java 查找元素最大值最低的数组,java,arrays,Java,Arrays,我有一个int[]数组的arraylist,我试图找到max元素最低的数组 例如,从数组[1,2,5]、[0,1,2]、[8,0,0]中,它将是数组[0,1,2],因为max元素是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 (li
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
}
}
}
int min=Integer.MAX\u值;
对于(int i=0;i您的代码在所有数组中找到最小值(如果您将list.get(i)[i]
的输入错误修复为list.get(i)[j]
),至少会这样)
您应该找到每个数组的最大元素,然后检查该最大值是否小于之前找到的所有最大值:
int minIndex = 0;
int min = Integer.MAX_VALUE;
for (int i=0; i<list.size(); i++) {
int max = Integer.MIN_VALUE;
for (int j=0; j<list.get(i).length; j++) { // find max element of current array
if (list.get(i)[j] > max) {
max = list.get(i)[j];
if (max > min) {
break; // we already know the max of this array is not the smallest max
}
}
}
if (max < min) { // check if the max of the current array is the smallest max so far
min = max;
minIndex = i; //index of the array in arraylist
}
}
int minIndex=0;
int min=整数最大值;
对于(int i=0;i min){
break;//我们已经知道这个数组的max不是最小的max
}
}
}
if(max
您可以尝试以下示例:
import java.util.Arrays;
import java.util.Comparator;
public class MaxMinArray {
public static int[] maxMinFromArray(int[][] arrays){
return Arrays.stream(arrays).min(Comparator.comparingInt(x -> Arrays.stream(x).max().orElse(0))).orElse(null);
}
}
我已经用你的例子进行了测试:):
作为for循环的替代方法,您可以尝试使用解决此问题。想法完全相同:
- 在每个子列表中查找最大元素李>
- 用于在最大元素中查找具有最小元素的子列表李>
代码较少,可以说很容易理解代码的意图。
您甚至可以使用Comparator::comparing
方法缩短代码:
List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
.stream()
.min(Comparator.comparing(Collections::max))
.get();
让我们更详细地看看这里发生了什么
List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
// lets get stream of list Stream<List<Integer>>.
.stream()
// find sublist which has minimum maximum element.
.min((a, b) ->
// a & b are sublist, for example: [1,2,5], [0,1,2]
// find maximum from a [1,2,5] which is [5]
a.stream().max(Integer::compare).get()
// compare maximum from a to maximum from b
.compareTo(
// find maximum from a [0,1,2] which is [2]
b.stream().max(Integer::compare).get()
)
// get minimum out of [5,2]
).get(); // [0, 1, 2]
我希望你觉得这有用 什么错误?什么是列表
?请创建一个数组,如果两个或多个数组具有相同的最低最大值,您打算返回什么数组?抱歉,没有错误,它被卡在循环中。List是数组的arraylist。@如果出现这种情况,我想返回两个或多个数组,但我首先只想找到一个。谢谢,不过我想我必须重新考虑我的方法,因为这对于我的数据量来说是非常缓慢的。我一定会在我的返工方法中使用你的解决方案。谢谢again@Jane在最坏的情况下,您必须测试所有数组的所有元素,但是您可以通过在max>min时中断内部循环来进行优化。我使用了您的代码,只是删除了ArrayList以摆脱嵌套循环。区别是分钟:)。谢谢
List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
.stream()
.min(Comparator.comparing(Collections::max))
.get();
List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
// lets get stream of list Stream<List<Integer>>.
.stream()
// find sublist which has minimum maximum element.
.min((a, b) ->
// a & b are sublist, for example: [1,2,5], [0,1,2]
// find maximum from a [1,2,5] which is [5]
a.stream().max(Integer::compare).get()
// compare maximum from a to maximum from b
.compareTo(
// find maximum from a [0,1,2] which is [2]
b.stream().max(Integer::compare).get()
)
// get minimum out of [5,2]
).get(); // [0, 1, 2]
Initial list is: [1,2,5], [0,1,2], [8, 0, 0]
find minimum list based on maximum:
min( max([1,2,5]), max([0,1,2]))
min( [5], [2])
[2] -> list [0,1,2] contains minimum maximum element so far, go the the next iteration
find minimum list based on maximum:
min( max([0,1,2]), max([8, 0, 0]) )
min( [2], [8])
[2] -> list [0,1,2] contains minimum maximum element so far,
there no other element in the stream, [0,1,2] is final result.