simple program using interface " iterable"

Discussion in 'Java' started by Thomas, Nov 12, 2007.

  1. Thomas

    Thomas Guest

    Hello, I have to write a simple program containing a list which would use a
    interface iterable or iterate (don't know exaclty whitch one it ). The list
    ( which would be programmed on my own) would return the iterator, which
    would use the standard
    methods as hasNext(), remove() and next(). I tried to find some examples
    over the internet and the Gosling book " The Java ..." but neither one help
    me. I need a simple template which would work or a link where it is
    explained.
     
    Thomas, Nov 12, 2007
    #1
    1. Advertising

  2. Thomas

    Lew Guest

    Thomas wrote:
    > Hello, I have to write a simple program containing a list which would use a
    > interface iterable or iterate (don't know exaclty whitch one it ). The list


    Did you mean Iterable, as in java.util.Iterable? (Spelling counts.)

    > ( which would be programmed on my own) would return the iterator, which
    > would use the standard
    > methods as hasNext(), remove() and next(). I tried to find some examples
    > over the internet and the Gosling book " The Java ..." but neither one help
    > me. I need a simple template which would work or a link where it is
    > explained.


    If you extend AbstractList you get Iterable for free.

    If you simply add "implements Iterable" to your class declaration (they did
    cover this in class right? You attended? You took notes?) and implement all
    the methods described in Iterable's Javadocs (you do read the Javadocs,
    right?), you're home free.

    --
    Lew
     
    Lew, Nov 12, 2007
    #2
    1. Advertising

  3. Thomas

    Lew Guest

    Lew wrote:
    > Did you mean Iterable, as in java.util.Iterable? (Spelling counts.)


    So does the right package name: java.lang.Iterable. Oops.

    I got confused with java.util.Iterator.

    --
    Lew
     
    Lew, Nov 12, 2007
    #3
  4. Thomas

    Thomas Guest

    U¿ytkownik "Lew" <> napisa³ w wiadomo¶ci
    news:...
    > Thomas wrote:
    >> Hello, I have to write a simple program containing a list which would use
    >> a interface iterable or iterate (don't know exaclty whitch one it ). The
    >> list

    >
    > Did you mean Iterable, as in java.util.Iterable? (Spelling counts.)
    >
    >> ( which would be programmed on my own) would return the iterator, which
    >> would use the standard
    >> methods as hasNext(), remove() and next(). I tried to find some examples
    >> over the internet and the Gosling book " The Java ..." but neither one
    >> help me. I need a simple template which would work or a link where it is
    >> explained.

    >
    > If you extend AbstractList you get Iterable for free.
    >
    > If you simply add "implements Iterable" to your class declaration (they
    > did cover this in class right?

    Yes I TOOK notes, but this topic wasn't covered and I have to write it on
    Thursday before the deadline and have six other
    curses so please be less sarcastic.

    This template does work :

    **********************************

    import java.util.Iterator;

    public class My_iterator implements Iterator {

    public My_iterator(){
    }


    public boolean hasNext() {
    return false;
    }

    public Object next() {
    return new Character('A');
    }

    public void remove() {

    }
    }
    ****************************************

    but this doesn't :
    ***************************************
    import java.util.Iterator;
    public class ListNode implements java.lang.Iterable {

    private String str;
    private int count = 0;
    public ListNode(){

    }

    public Iterator iterator(){};
    }
    *****************************************
    since I got:
    cannot resolve class Iterator
    cannot resolve class Iterable






    You attended? You took notes?) and implement all
    > the methods described in Iterable's Javadocs (you do read the Javadocs,
    > right?), you're home free.
    >
    > --
    > Lew
     
    Thomas, Nov 13, 2007
    #4
  5. Thomas wrote:
    > but this doesn't :
    > ***************************************
    > import java.util.Iterator;
    > public class ListNode implements java.lang.Iterable {
    >
    > private String str;
    > private int count = 0;
    > public ListNode(){
    >
    > }
    >
    > public Iterator iterator(){};
    > }
    > *****************************************
    > since I got:
    > cannot resolve class Iterator
    > cannot resolve class Iterable


    I get:

    Z.java:10: missing return statement
    public Iterator iterator(){};
    ^
    1 error

    which seems as very descriptive error message.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 13, 2007
    #5
  6. Thomas

    Lew Guest

    Arne Vajhøj wrote:
    > Thomas wrote:
    >> but this doesn't :
    >> ***************************************
    >> import java.util.Iterator;
    >> public class ListNode implements java.lang.Iterable {
    >>
    >> private String str;
    >> private int count = 0;
    >> public ListNode(){
    >>
    >> }
    >>
    >> public Iterator iterator(){};
    >> }
    >> *****************************************
    >> since I got:
    >> cannot resolve class Iterator
    >> cannot resolve class Iterable

    >
    > I get:
    >
    > Z.java:10: missing return statement
    > public Iterator iterator(){};
    > ^
    > 1 error
    >
    > which seems as very descriptive error message.


    That last semicolon has to go, also.

    --
    Lew
     
    Lew, Nov 13, 2007
    #6
  7. Lew wrote:
    > That last semicolon has to go, also.


    It is not necessary, but it compiles with it.

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Nov 13, 2007
    #7
  8. Thomas

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> That last semicolon has to go, also.

    >
    > It is not necessary, but it compiles with it.


    Are you saying that it should not go?

    If so, why should it stay?

    --
    Lew
     
    Lew, Nov 13, 2007
    #8
  9. Lew wrote:
    > Arne Vajhøj wrote:
    >> Lew wrote:
    >>> That last semicolon has to go, also.

    >>
    >> It is not necessary, but it compiles with it.

    >
    > Are you saying that it should not go?


    As stated - it compiles with it.

    > If so, why should it stay?


    I think the original poster has better things
    to spend his time on learning than optional
    semicolons or not.

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Nov 13, 2007
    #9
  10. Thomas

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> Arne Vajhøj wrote:
    >>> Lew wrote:
    >>>> That last semicolon has to go, also.
    >>>
    >>> It is not necessary, but it compiles with it.

    >>
    >> Are you saying that it should not go?

    >
    > As stated - it compiles with it.
    >
    >> If so, why should it stay?

    >
    > I think the original poster has better things
    > to spend his time on learning than optional
    > semicolons or not.


    Ok.

    I think it's a bad idea to put spurious empty constructor lines in strange
    places in one's source.

    It's a bad habit to start, and the beginning is when it's easiest to form
    habits for good or ill.

    --
    Lew
     
    Lew, Nov 13, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Remi Bastide
    Replies:
    5
    Views:
    13,576
    elizas
    Mar 29, 2010
  2. Googmeister

    Whay aren't Strings Iterable?

    Googmeister, Oct 9, 2005, in forum: Java
    Replies:
    13
    Views:
    991
    Mike Schilling
    Oct 10, 2005
  3. Leif K-Brooks

    Word for a non-iterator iterable?

    Leif K-Brooks, Feb 6, 2005, in forum: Python
    Replies:
    5
    Views:
    389
    Terry Reedy
    Feb 7, 2005
  4. Robert Klemme
    Replies:
    2
    Views:
    609
    Robert Klemme
    Oct 16, 2006
  5. Replies:
    2
    Views:
    366
    James Stroud
    Dec 10, 2008
Loading...

Share This Page