• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D Array removing Duplicates

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm importing a txt file, and then filling the 2D array with the points, all that is working fine. I can't for the life of me get it working where I can find duplicate points and then basically create a new array with only single points, no duplicates.

Since they're coordinates I only want to remove a duplicate POINT, so both X and Y have to match. If only X or only Y match, the points should stay.

I'm using the processing IDE.


 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first step should be to figure out what you want to end up with. An "array"? Okay... what kind of an array? And what should be in it?
 
dustin kab
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to come up with a 2D array of points that each POINT is an individual. So there are no duplicate points, if there is a cleaner way of simply removing those points and shrinking that Array I would like to do that.

(by POINT I mean, both [x][y] are not the same as any other [x][y])

Ultimate Goal:
- Create a 2D array that contains no duplicate points.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dustin kab wrote:I'm importing a txt file, and then filling the 2D array with the points, all that is working fine. I can't for the life of me get it working where I can find duplicate points and then basically create a new array with only single points, no duplicates.

Since they're coordinates I only want to remove a duplicate POINT, so both X and Y have to match. If only X or only Y match, the points should stay.

I'm using the processing IDE.


Welcome to the Ranch

Why don't you put the points into a Set (eg a Tree) as you read them from the text file. Using the return value from the add() method tells you that you have a unique value, and you can add it to your array.

[pedantic mode]There is no such thing as a 2D array in Java™, only an array of arrays.[/pedantic mode]
 
dustin kab
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks! I've never worked with sets before.

In order to get both points I figured I had to create 2 separate sets.



I got it going converting 2 single arrays into an array of arrays:


Thanks a ton, if you know how to make this more efficient I'm all ears.

 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your problem would be a lot less confusing if you just said "I have an array of points and I want to remove all of the duplicates, so I end up with an array of unique points". This business of using a two-dimensional array of floats where each row represents a point (by having exactly two columns) just adds a layer of obfuscation to your problem description.

 
dustin kab
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tips, I'll be more clear next time. Either way, it's working now, thanks for the help.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic