1 // Initializing array of integers
2 Integer[] num = { 2, 4, 7, 5, 9 };
3// using Collections.min() to find minimum element
4 // using only 1 line.
5 int min = Collections.min(Arrays.asList(num));
6
7 // using Collections.max() to find maximum element
8 // using only 1 line.
9 int max = Collections.max(Arrays.asList(num));
1public static double arrayMax(double[] arr) {
2 double max = Double.NEGATIVE_INFINITY;
3
4 for(double cur: arr)
5 max = Math.max(max, cur);
6
7 return max;
8}
1public static double max(double[] arr) {
2 double result = Double.NEGATIVE_INFINITY;
3 for (int i = 0; i < arr.length; ++i) {
4 if (result < arr[i]) result = arr[i];
5 }
6 return result;
7}
1Integer[] num = { 2, 4, 7, 5, 9 };
2 // using Collections.min() to find minimum element
3 // using only 1 line.
4 int min1 = Collections.min(Arrays.asList(num));
5
6 // using Collections.max() to find maximum element
7 // using only 1 line.
8 int max1 = Collections.max(Arrays.asList(num));
9
10 System.out.println(min1); // 2
11 System.out.println(max1); // 9
1 public static void main(String[] args) {
2 int [] arr = {100,200,300,50,40,-10,-50};
3 int max = Integer.MIN_VALUE;
4 for(int each: arr)
5 if(each > max)
6 max = each;
7 System.out.println(max); // 300