max and min array number in java code example
Example 1: max in array java
Integer[] num = { 2, 4, 7, 5, 9 };
int min = Collections.min(Arrays.asList(num));
int max = Collections.max(Arrays.asList(num));
Example 2: java max array
public static String max(String[] arr) {
String result = arr[0];
for (int i = 1; i < arr.length; ++i) {
if (result.compareTo(arr[i]) < 0) result = arr[i];
}
return result;
}
Example 3: java max array
public static double max(double[] arr) {
double result = arr[0];
for (int i = 1; i < arr.length; ++i) {
if (result < arr[i]) result = arr[i];
}
return result;
}
Example 4: java max array
public static double max(double[] arr) {
double result = Double.NEGATIVE_INFINITY;
for (int i = 0; i < arr.length; ++i) {
if (result < arr[i]) result = arr[i];
}
return result;
}
Example 5: max array number in java
Integer[] num = { 2, 4, 7, 5, 9 };
int min1 = Collections.min(Arrays.asList(num));
int max1 = Collections.max(Arrays.asList(num));
System.out.println(min1);
System.out.println(max1);
Example 6: max and min array number in java
Integer[] num = { 2, 4, 7, 5, 9 };
int min1 = Collections.min(Arrays.asList(num));
int max1 = Collections.max(Arrays.asList(num));
System.out.println(min1);
System.out.println(max1);