Results 1 to 10 of 10
- 04-02-2011, 12:03 AM #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:
The importing is working fine, but here's where the problem lies.Java 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]); }
Goal: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])) { list.add(tempPoints[r][0]); list2.add(tempPoints[r][1]); } } } }
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] //yLast edited by dusker; 04-02-2011 at 12:15 AM.
- 04-02-2011, 12:38 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 12:44 AM.
- 04-02-2011, 12:49 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.
- 04-02-2011, 01:14 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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, 06:41 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
set.add(point(coordinates[c][0],coordinates[c][1]));
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
Note the ... is code that you'll need to fill in.Java Code:set.add(new Point(....));
- 04-02-2011, 06:51 PM #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"
-
This has absolutely nothing to do with the IDE, but rather all to do with bugs in your code.
What class are you using for Point? Have you imported it? These are basic Java issues. Have you gone through the introductory tutorials yet?For instance:
Point originOne = new Point(23, 94);
returns: Cannot find a class or type named "Point"
- 04-02-2011, 06:57 PM #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 07:06 PM.
-
Similar Threads
-
find the unique values of a double array
By tyang in forum New To JavaReplies: 3Last Post: 09-11-2011, 02:47 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Accessing elements of an ArrayList which contains 2D arrays
By Willi in forum New To JavaReplies: 5Last Post: 01-18-2010, 07:00 AM -
Unique element in an array
By revathi17 in forum New To JavaReplies: 2Last Post: 12-31-2007, 08:44 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks