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

In summary, The method being discussed is used to add an item to a heap. The problem at hand is receiving an error when trying to compare values of generics. It is not the correct way to compare E values and it is difficult to find resources on how to properly compare them. A potential solution could be to use Collections.sort, a Java standard for dealing with this issue.
  • #1
trouty323
24
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
  • #2
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.
 

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

1. What does the error "The operator > is undefined for the argument type(s) E, E" mean?

This error message indicates that there is an issue with using the greater than (>) operator in a piece of code involving the type "E". This could be due to a mismatch between the data types being compared or a missing import for a necessary library or class.

2. How can I fix this error?

To fix this error, you will need to ensure that the data types being compared are compatible with the greater than (>) operator. You may also need to import the appropriate library or class that contains the definition for the type "E".

3. Can this error occur in any Java program?

Yes, this error can occur in any Java program that uses the greater than (>) operator and involves the type "E". It is important to carefully check and validate data types when using comparison operators in Java to prevent this error.

4. Are there any common causes for this error?

One common cause for this error is a mismatch between the data types being compared. For example, trying to use the greater than (>) operator to compare a String and an Integer will result in this error. Another common cause is forgetting to import the necessary library or class for the type "E".

5. Is there a way to avoid this error in my Java code?

Yes, you can avoid this error by carefully checking and validating the data types being compared when using the greater than (>) operator. It is also helpful to make sure all necessary libraries or classes are imported before using the type "E" in your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top