I am writing a program for extra practice from my book on finding the max element in array using Java Generics. My Code so far is:

``````public class GenericMax {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3}; //Creates array of integers
System.out.println(max(numbers));

String[] words = {"red", "green", "blue"}; //Creates an array of strings
System.out.println(max(words));

Circle[] circles = {new Circle(3), new Circle(2.9), new Circle(5.9)}; //creates an array of circles
System.out.println(max(circles));
}

static class Circle implements Comparable<Circle> { //Circle object implements Comparable to compare with Circle
double radius;
public Circle (double radius){
this.radius = radius; //sets value of radius from main method for radius
}

@Override
public int compareTo(Circle c) {
if (radius < c.radius)
return -1;
else if (radius == c.radius)
return 0;
else
return 1;
}

@Override
public String toString() {
return "Circle radius: " + radius;
}
}

public static <E extends Comparable<E>> E max(E[] list) {
E max = list; //sets the first value in the array as the current maximum
for (int i = 1; i <= list.length; i++) {
if (list[i] > max) {
max = list[i];
}

}
return max;
}
}
``````

I am getting a compile error at "if (list[i] > max)".
What have I done wrong? There is no complie error anywhere else in the code

## Recommended Answers

The `>` operator is only defined for primitive types. To compare `Comparable` objects, use the `compateTo` method instead.

The > operator only works for numeric primitives. Depending on the type of the generic arguent that could mean
Circle1 > Circle2 (etc)
Because all your generics implement Comparable. you must call `compareTo` rather than using numeric operators

## All 5 Replies

The `>` operator is only defined for primitive types. To compare `Comparable` objects, use the `compateTo` method instead.

The > operator only works for numeric primitives. Depending on the type of the generic arguent that could mean
Circle1 > Circle2 (etc)
Because all your generics implement Comparable. you must call `compareTo` rather than using numeric operators

So is this the appropriate method operator? :

``````public static <E extends Comparable<E>> E max(E[] list) {
E max = list; //sets the first value in the array as the current maximum
for (int i = 1; i < list.length; i++) {
if (list[i].compareTo(max) > 0) //if value in list[i] is greater than max and is positive(greater than 0), the max value is replaced by list[i] {
max = list[i];
}

}
return max;
}
``````

My personal preference is to use Guava's ComparisonChain. Its a much more subtle way of doing the comparison.

Maybe a better option, but for a new learner I would always advise sticking to the standard API if possible - 3rd party libraries do add an extra level of complication just when it's least needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.