# Array of Arrays all unique elements

• 04-02-2011, 01:03 AM
dusker
Array of Arrays all unique elements
I think this is a "Unique" problem I have here, seriously though.

I have an array of coordinates that i've imported from a text file:

Code:

```float[][] tempPoints; String[] strLines = loadStrings(exportedCoordinates);   tempPoints = new float[PointsCount][2];     for(int r=0;r<strLines.length;r++)   {     String[] coordsList = split(strLines[r], ',');         tempPoints[r][0] = float(coordsList[0]);     tempPoints[r][1] = float(coordsList[1]);   }```
The importing is working fine, but here's where the problem lies.

Code:

```void findDuplicates() {   for(int r=0;r<tempPoints.length;r++)   {        for(int p=0;p<tempVPoints.length;p++)     {         if(!list.contains(tempVPoints[p][0]) && !list2.contains(tempPoints[p][1]))         {             list.add(tempPoints[r][0]);             list2.add(tempPoints[r][1]);         }     }   } }```
Goal:

I need to create a 2D array that contains all unique sets of coordinates. (coordinates being: coord[i][0] //x coord[i][1] //y )

I initially thought I would use a set, but the problem with that is I need to make sure that both of the elements never repeat together. They can be individually there, but no set of coordinates can repeat in the array.

example of why a SET does not work for this problem:
coordinate[400][0] //x
coordinate[300][1] //y

coordinate[400][0] //x -- 400 appears twice, thus it is removed prematurely.
coordinate[100][1] //y
• 04-02-2011, 01:38 AM
pbrockway2
Quote:

I need to create a 2D array that contains all unique sets of coordinates.

You are best to use a set for this problem as a set exactly models your condition of having no duplicates.

Two new arrays containing the same coordinates will not appear as the same when compared with equals() (because they're different arrays...) so don't use arrays. Instead define your own type for the points. Or use one that already exists like Point.

---------------

If there is some nonobvious reason why arrays must be used - like it's an assignment or something - say.
• 04-02-2011, 01:49 AM
dusker
Thanks for the quick response. Oh no it's not an assignment, it's just for something I'm messing around with at home. You gave me a good lead though, wasn't 100% clear on sets as I had never used them before.

Thanks again I'll see how it goes.
• 04-02-2011, 02:14 AM
pbrockway2
OK - post back if you have any problems.

I realise that I was considering points with integer coordinates. There are also a builtin point classes that use floating point coordinates - double (rather than float) is usually the preferred type for such things.
• 04-02-2011, 07:41 PM
dusker

When I attempt to do that it says "type Set is not applicable for the arguments (void)"

What would be the next approach? I tried an arrayList, but I got the same thing.
• 04-02-2011, 07:46 PM
Fubarable
Quote:

Originally Posted by dusker

When I attempt to do that it says "type Set is not applicable for the arguments (void)"

What would be the next approach? I tried an arrayList, but I got the same thing.

The next approach would be to use Sets correctly which you are not doing as you're calling a method point(...) which returns void rather than adding an object to the collection. I'm not surprised that this code wouldn't work with an ArrayList either since all collections require that you add objects.

So don't call a void returning method inside of your set.add(...) but rather add a new object into the set. How to do this depends on your code, but perhaps you want to call

Code:

`set.add(new Point(....));`
Note the ... is code that you'll need to fill in.
• 04-02-2011, 07:51 PM
dusker
o okay thanks, I'm using the Processing IDE, so they seem to do things a bit differently.

For instance:
Point originOne = new Point(23, 94);

returns: Cannot find a class or type named "Point"
• 04-02-2011, 07:55 PM
Fubarable
Quote:

Originally Posted by dusker
o okay thanks, I'm using the Processing IDE, so they seem to do things a bit differently.

This has absolutely nothing to do with the IDE, but rather all to do with bugs in your code.

Quote:

For instance:
Point originOne = new Point(23, 94);

returns: Cannot find a class or type named "Point"
What class are you using for Point? Have you imported it? These are basic Java issues. Have you gone through the introductory tutorials yet?
• 04-02-2011, 07:57 PM
dusker
I actually got it working, thanks for the help though. I do realize processing is it's own language, but it is based on Java so I thought I'd reach out for some help.
• 04-02-2011, 08:21 PM
Fubarable
Quote:

Originally Posted by dusker
I actually got it working, thanks for the help though. I do realize processing is it's own language, but it is based on Java so I thought I'd reach out for some help.

You're asking questions about a non-Java language in a Java forum? Thanks for telling me now.