Results 1 to 6 of 6
  1. #1
    powerpravin is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Exclamation Problem with Iterator class

    I have created a class 'Gerbil' and then created a hash set of the same class in class 'Program
    -----------------------------------------
    class Gerbli
    {
    //Contents of class
    }
    class Program
    {
    Set<Gerbil> hs = new HashSet<Gerbil>();

    void addtoHashSet(Gerbil obj)
    {
    hs.add(obj);
    }

    void get()
    {
    Iterator itr = hs.iterator();
    while(itr.hasNext())
    {
    Gerbil obj=itr.next();//--->ERROR

    }
    }

    }

    ------------------------
    but while compiling the code, the compiler is giving an error at statement

    Gerbil obj=itr.next();


    Error: incompatible types
    found : java.lang.Object
    required: Gerbil

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

    But when I tried with 'getClass' method on itr.next(), it returns 'Gerbil class'


    Messed:confused:
    Please help:mad:
    Thanks!!!

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

    Default

    Don't use an Iterator object; use an Iterator<Gerbil> object; the first object is a 'raw' object and returns Object type objects; the second one returns Gerbil type objects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your iterator is not typed so Java won't let you just assume that it's going to give Gerbli type objects. Better type it with
    Java Code:
    Iterator<Gerbli> itr = hs.iterator();
    Your spelling also looks suspicious. Is it Gerbil or Gerbli?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    ... and Jos is sneaky ...

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    ... and Jos is sneaky ...
    Nope, just amazingly fast; my laptop's keyboard is still smoking ...

    kind regards,

    Jos (<--- voooom! ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    powerpravin is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Your iterator is not typed so Java won't let you just assume that it's going to give Gerbli type objects. Better type it with
    Java Code:
    Iterator<Gerbli> itr = hs.iterator();
    Your spelling also looks suspicious. Is it Gerbil or Gerbli?

    Using
    Java Code:
    Iterator<Gerbli> itr = hs.iterator();
    worked. Thanks for the help.

    By the way it's Gerbil. At top, I misspelled it.

Similar Threads

  1. iterator inner class in minheap
    By anders10 in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 02:52 PM
  2. Replies: 9
    Last Post: 12-15-2010, 02:38 AM
  3. Iterator next import class?
    By daydreamer in forum New To Java
    Replies: 7
    Last Post: 10-04-2010, 11:32 PM
  4. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Tags for this Thread

Posting Permissions

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