# Thread: Array of Arrays all unique elements

1. Member
Join Date
Apr 2011
Posts
5
Rep Power
0

## 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:

Java Code:
```float[][] tempPoints;

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.

Java 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]))
{
}
}
}
}```
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
Last edited by dusker; 04-02-2011 at 01:15 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.
Last edited by pbrockway2; 04-02-2011 at 01:44 AM.

3. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.

5. Member
Join Date
Apr 2011
Posts
5
Rep Power
0

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.

6. 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

Java Code:
`set.add(new Point(....));`
Note the ... is code that you'll need to fill in.

7. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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"

8. 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.

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?

9. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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.
Last edited by dusker; 04-02-2011 at 08:06 PM.

10. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•