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 3 of 3

Thread: Iterator & ArrayList

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Iterator & ArrayList

    Hi there,
    I have an ArrayList<String> and i'm using an Iterator loop to display all the strings one by one
    public void displayAllTitles(){
            Iterator<String> it = bestSellersNames.iterator();
            while(it.hasNext()){
                it.next();
                System.out.println(it);
            }
    }
    there's a problem because when I call the method it displays the memory address of the string.
    How I can display the string itself without using another method?

    Thanks
    Last edited by raabhim; March 24th, 2012 at 05:18 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Iterator & ArrayList

    when I call the method it displays the memory address of the string
    No it doesn't. String has a toString() method (read the Java SE API doc at Object.toString() and String.toString()). Your code says:

    System.out.println(it);

    What is 'it'? It's not a String! The String you want is returned on the line before by it.next()

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

    raabhim (March 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Iterator & ArrayList

    Right! Silly me...
    it's working now
    Thanks for your help!
    public void displayAllTitles(){
            Iterator<String> it = bestSellersNames.iterator();
            while(it.hasNext()){
            String b =  it.next();
                System.out.println(b);
            }
    }
    Last edited by raabhim; March 24th, 2012 at 05:18 PM.

Similar Threads

  1. Help With Iterator code... please help me..
    By basketball8533 in forum Collections and Generics
    Replies: 0
    Last Post: November 20th, 2011, 11:44 PM
  2. stuck on Iterator
    By Mjall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 11:00 PM
  3. iterator help needed
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 12:32 PM
  4. Iterator Problem
    By chrisych in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 10th, 2010, 01:29 PM
  5. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM