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 max(double[] arr) {
2 double result = arr[0];
3 for (int i = 1; i < arr.length; ++i) {
4 if (result < arr[i]) result = arr[i];
5 }
6 return result;
7}
1public static String max(String[] arr) {
2 String result = arr[0];
3 for (int i = 1; i < arr.length; ++i) {
4 if (result.compareTo(arr[i]) < 0) result = arr[i];
5 }
6 return result;
7}
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}
1import java.util.Random;
2
3public class Main {
4
5public static void main(String[] args) {
6 int a[] = new int [100];
7 Random rnd = new Random ();
8
9 for (int i = 0; i< a.length; i++) {
10 a[i] = rnd.nextInt(99-0)+0;
11 System.out.println(a[i]);
12 }
13
14 int max = 0;
15
16 for (int i = 0; i < a.length; i++) {
17 a[i] = max;
18
19
20 for (int j = i+1; j<a.length; j++) {
21 if (a[j] > max) {
22 max = a[j];
23 }
24
25 }
26 }
27
28 System.out.println("Max element: " + max);
29}
30}