97,582 views
15 votes
15 votes
Write a static generic method PairUtil.minmax that computes the minimum and maximum elements of an array of type T and returns a pair containing the minimum and maximum value. Require that the array elements implement the Measurable interface of Section 10.1.2.

User Nikitc
by
3.0k points

1 Answer

25 votes
25 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It is hard to fully create the code without the rest of the needed code including the T class and the Measurable interface. Regardless the following code can be implemented if you have that code available.

public static T minmax(ArrayList<T> mylist) {

T min = new T();

T max = new T();

for (int x = 0; x < mylist.size(); x++) {

if (mylist.get(x) > max) {

max = mylist.get(x);

} else if (mylist.get(x) < min) {

min = mylist.get(x);

}

}

return (min, max);

}

User Bcar
by
2.7k points