Java Error: The operator > is undefined for the argument type(s) E, E

Click For Summary
SUMMARY

The discussion addresses a Java error encountered when attempting to compare generic types using the operator '>'. The error message "The operator > is undefined for the argument type(s) E, E" indicates that generics in Java do not support direct comparison without a defined ordering. The correct approach is to implement the Comparable interface or use a Comparator to facilitate comparisons between generic types. The user references the Collections.sort method as a standard solution for handling comparisons of generic values.

PREREQUISITES
  • Understanding of Java generics and type parameters
  • Familiarity with the Comparable interface in Java
  • Knowledge of the Comparator interface for custom sorting
  • Basic understanding of heap data structures and their operations
NEXT STEPS
  • Implement the Comparable interface in your generic class to enable comparison
  • Explore the use of Comparator for custom sorting of generic types
  • Study Java Collections Framework, focusing on sorting methods
  • Review heap data structure implementations in Java for better understanding
USEFUL FOR

Java developers, software engineers working with generics, and anyone implementing data structures like heaps in Java.

trouty323
Messages
23
Reaction score
0


Hello. The method here is to add an item to a heap. As the title states, I am getting the error "The operator > is undefined for the argument type(s) E, E" in the parenthesis after the while. I assume this is not the correct way to compare E values. Does anybody know what would be the correct way?

Code:
	public boolean add(E item) {
		data[size] = item;
		child = size - 1;
		parent = (child-1) / 2;
		
		while (parent >= 0 && data[parent] > data[child]) {
			// swap data[parent] and data[child]
			child = parent;
			parent = (child - 1) / 2;
		}
	}
 
Physics news on Phys.org
Anyone? data[] is an array that holds generics. I should have said that instead of saying "E." I can't seem to find a decent website that explains how to compare generic values.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K