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

Click For Summary
The error "The operator > is undefined for the argument type(s) E, E" occurs because Java generics do not support direct comparison using operators like >. To compare generic values, a comparator must be used, typically by implementing the Comparable interface or using a Comparator class. The discussion highlights the need for a proper comparison method for generic types, referencing the Collections.sort method as a standard approach. The user is seeking guidance on how to implement this correctly in their heap addition method. Understanding how to work with generics and comparisons is essential for resolving this issue.
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
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K