Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Iterator, with ArrayList

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Iterator, with ArrayList

    I get compile error when trying to use method of something i got off iterator.next()

    first off, I have a class named word, it has a method called getWord which returns a String.

    import //all needed packages
    public class unique
    {
     
    ArrayList<word> words = new ArrayList<word>();
     
    public unique(.........)
    {
    //filling ArrayList with objects of type word.
    }
     
    private boolean checkForWord(String token)
    {
     
    Iterator itr = words.iterator();
    String temp;
     
    while(itr.hasNext())
        {
            temp = itr.next().getWord();
            [COLOR="Red"]//COMPILE ERROR ON THE LINE ABOVE[/COLOR]:-q:-q:-q
            if(temp.equals(token))
            return true;
        }
     
        return false;
    }

    public class word
    {
    String token;
     
    public word (String token)
    {
        this.token = token;
    }
    public String getWord()
    {
        return token;
    }
     
    }

    The compile error I receive is , cannot find method getWord()

    Any ideas?

    I suspect it may have something to do with iterator POSSIBLY returning type OBJECT ..but even if this is the case, how do I convert this back to a "usable" object of type word?
    Last edited by rsala004; October 24th, 2009 at 10:24 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Iterator, with ArrayList

    Add a cast.

    temp = ((word)itr.next()).getWord();

    You might get a warning that says this is a potentially unsafe cast, but since you know that the iterator can only return a word, you can ignore the warning.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    rsala004 (October 25th, 2009)

  4. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: Iterator, with ArrayList

    xD

    I found a solution a little while ago :O

    by setting the type of the iterator, didnt know i could do it.

    Iterator<word> itr = words.iterator();

    thanks though

  5. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Iterator, with ArrayList

    You can also use a special for loop syntax that automatically handles iterators.

    for (word w : words) {
        System.out.println(w.getWord());
    }

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  3. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM