Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you remove duplicate Point Objects in an array?

 
H Lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Point2D class that stores information about the x and y coordinates of a point object and it also contains getters.

When I create an array of point objects I dont know how i can remove duplicates if there are any?

Any other ways I can remove cuplicate Point objects from an array?

Thanks
[ March 31, 2008: Message edited by: H Lee ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"go999",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Are you checking for similar x and y coordinates or the same point?

If you are checking for similar points, a much easier approach is to use "sets" in java to eliminate duplication of elements.

hope this helps..
 
H Lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im checking if there are two points that are equal to each other.
By equal I mean that their X coordinates must be equal and their Y coordinates must be equal.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are Collections that can remove duplicates (check api in java.util I am not sure if ArrayList removes duplicates but there is something that will, I am pretty sure that is what they meant by sets) they will tell if it is a duplicate usually by the equals() method found in object. If that is not what you want then you can subclass Points2D and override the equals() method to check for the X and Y.

Also in java.awt.Point (a subclass of Point2D) there is already an overrided equals() method.
[ March 31, 2008: Message edited by: colton peterson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic