Results 1 to 5 of 5
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default get index from an array (very frustrating)

    Hey ,i'm trying to write a method that get a point from a user and returns the index value of it, and if there isn't such point return -1, for some reason it doesn't work..

    public int findPoint (int x,int y){
    Point pnt = new Point (x,y);
    int i;
    int index=-1;
    for (i=0; i<_vertices.length; i++){
    if (_vertices [i] == pnt)

    index = i;
    }
    return index;
    }
    }

    //Thank you for helping.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Do this instead:

    Java Code:
    if (_vertices [i].equals(pnt))
    You can't compare two objects for equality using the == operator.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Do this instead:

    Java Code:
    if (_vertices [i].equals(pnt))
    You can't compare two objects for equality using the == operator.

    kind regards,

    Jos
    Haha I like the part where u tell him why he can't simply use ==. But seriously u can't use == because it compares references. You need to use the equals method to compare objects (u may need to write ur own equals method as the provided one is somewhat naive)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bertstar View Post
    Haha I like the part where u tell him why he can't simply use ==. But seriously u can't use == because it compares references. You need to use the equals method to compare objects (u may need to write ur own equals method as the provided one is somewhat naive)
    The standard Point.equals( ... ) method is perfectly suited for the job; no need to write your own. The OP creates a new Point for every comparison so the == operator will always return false.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I agree that the provided equals method should be fine here, but my post was more to point out the equals method is frequently overriden.

Similar Threads

  1. Using variable as array index
    By SalParadise in forum New To Java
    Replies: 9
    Last Post: 11-16-2010, 09:56 PM
  2. Please help setting up Tomcat. Very frustrating.
    By wombatvvv in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 09-12-2010, 05:56 AM
  3. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  4. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 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
  •