AI Features

Solution Review: Finding Max in an Array

In this review, solution of the challenge 'Finding Max in an Array' from the previous lesson is provided.

We'll cover the following...

Given solution

Java
class FindMax {
public static < T extends Comparable < T >> T array_max(T data[], int n) {
T max = data[0];
int i;
for (i = 1; i < n; i++) {
if (max.compareTo(data[i]) < 0) {
max = data[i];
}
}
return max;
}
public static void main( String args[] ) {
Integer[] inputs1 = {2,8,20,3,4};
Double[] inputs2 = {2.7,3.8,5.5,6.7,9.7};
System.out.println(array_max(inputs1,5));
System.out.println(array_max(inputs2,5));
}
}

Explanation

  • public static <T ...