public static int getMin(int[] arr, int min,int max,int a){
Integer[] test = Arrays.stream(arr).boxed().toArray(Integer[]::new);
List<Integer> list =null;
list = new ArrayList(Arrays.asList(test));
list.removeAll(Collections.singleton(0));
min = Collections.min(list);
max = Collections.max(list);
if(a == 0) {
return min;
} else {
return max;
}
}
List item
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:862)
at java.util.Collections.min(Collections.java:596)
at Solution.getMin(Solution.java:47)
What is the reason for this exception?
0elements does your list have?IntStream is = Arrays.stream(arr).filter(i -> i != 0); OptionalInt result = a == 0? is.min(): is.max();Then, you have to decide what to do if the result is empty, e.g.return result.orElse(0);