Results 1 to 10 of 10
  1. #1
    dusker is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default 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;
    
    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.

    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]);
             }
         }
      }
    }
    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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

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

    Default

    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. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #5
    dusker is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dusker View Post
    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

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

  7. #7
    dusker is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dusker View Post
    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. #9
    dusker is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dusker View Post
    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.

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 03:47 PM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  3. Replies: 5
    Last Post: 01-18-2010, 08:00 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 09:44 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 PM

Posting Permissions

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