I have this code:
private Point2D.Double getMedianX(Point2D.Double[] arr, int left, int right) { //Write codes here int center = (left + right) / 2; // order left & center if (arr[left] > arr[center]) swap(arr ,left, center);
What I wanted to do is use the > arithmetic operation in Point2D.Double object. But as the following error says I cannot use arithmetic operators in Point2D.Double elements.
Operator '>' cannot be applied to 'java.awt.geom.Point2D.Double', 'java.awt.geom.Point2D.Double'
How can I modify my code to work with > operator and other aritmetic operators without changing the input elements of the function getMedianX
?
Advertisement
Answer
The error is telling you exactly what is wrong. You can’t use arithmetic operators on Point2D.Double objects, and this makes sense since a Point2D
represents both an X and a Y value in 2D space — which one are you comparing?
You will need to extract the double values that the Point2D object holds, which as per the API will require your calling either .getX()
or .getY()
(or both), and then use these in your arithmetic comparisons.