Results 1 to 6 of 6
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Implement an Iterator

    I need some help with a programming assignment. I have to use an array list and an integer field to implement an iterator(with hasNext() and next()) etc.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What are you having trouble with? What have you tried?

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    i dont understand what it is asking for exactly. I know what an iterator is, but not sure how I could implement one.


    So, if I had a list L, that I wanted to iterate over

    Java Code:
    int i=0;
    List L = new List();
    L.add("1");
    L.add("2");
    
    Iterator itr = L.iterator();
    
    while(itr.hasNext()){
    Object nextItem = itr.next();
    **Do Processing Here on the listItem**
    i++;
    }
    Then at this point, I would use i to represent the number of elements in my list?? I'm supposed to use some pseudo code.
    Last edited by sehudson; 03-13-2011 at 11:52 PM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    that looks pretty good, try putting a print statement after the loop to print the final value of i to see if it counted the items in the list.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    I cant implement a List since its abstract(forgot) so I set:

    List L = new LinkedList();

    put in the println and it indeed works.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you solved it please mark your thread solved with the thread tools. If you have more questions feel free to ask.

Similar Threads

  1. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 12:17 PM
  2. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 02:12 PM
  3. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  4. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 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
  •