Results 1 to 8 of 8
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Question on .iterator()

    From the API ( Class ArrayList: )

    public Iterator<E> iterator()
    Returns an iterator over the elements in this list in proper sequence.
    My question is:

    Iterator is an Interface. And as far as I know, there can't be objects of type interface.

    So this method returns an object, that can be referenced by the Iterator interface. But what is the exact type of the object ?

    Is it an ArrayList ?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on .iterator()

    When I say .getClass() I get the output:

    class java.util.AbstractList$Itr


    which tells me nothing..

    AbstractList is an abstract class, as far as I know you can't have objects of this type as well. And I think $Itr means an inner - class of this abstract class?

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

    Default Re: Question on .iterator()

    Quote Originally Posted by fatabass View Post
    Iterator is an Interface. And as far as I know, there can't be objects of type interface.

    So this method returns an object, that can be referenced by the Iterator interface. But what is the exact type of the object ?

    Is it an ArrayList ?
    The class you got that Iterator from returns an object of a class that implements the Iterator interface; the class you got that Iterator from implements the Iterable interface (preferably). That actual class that implements the Iterator interface is none of our business, as long as it does what it has to do (and no, it isn't an ArrayList).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Question on .iterator()

    Just so you know what this means (as it can be helpful, albeit rarely):
    class java.util.AbstractList$Itr

    If you look at the source code for AbstractList you will see, defined inside it, a concrete class that implements the Iterator interface, called Itr.
    So the $ symbol means "inner class", followed by the name of the inner class.
    If you saw "class java.util.AbstractList$1" that is the first anonymous inner class.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on .iterator()

    There is also a constructor in the Abstract Class that says:

    protected AbstractList()
    Sole constructor. (For invocation by subclass constructors, typically implicit.)


    What is a constructor in an Abstract class used for ?

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on .iterator()

    I found this in the source file :

    private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
    cursor = index;
    } ( ... continued..)

    This is not included in the API right ?
    But why not ?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Question on .iterator()

    Quote Originally Posted by fatabass View Post
    There is also a constructor in the Abstract Class that says:

    protected AbstractList()
    Sole constructor. (For invocation by subclass constructors, typically implicit.)


    What is a constructor in an Abstract class used for ?
    It's still a class, so it still has stuff that needs constructing (it's own attributes for example).

    Quote Originally Posted by fatabass View Post
    I found this in the source file :

    private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
    cursor = index;
    } ( ... continued..)

    This is not included in the API right ?
    But why not ?
    Because, as Jos says, it's not something people are supposed to use directly.
    Indeed, since it's private, they can't.
    So, in the same way private methods do not appear in an API, private classes don't either.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. DIFFERENCE BETWEEN ITERATOR AND iterator( )
    By mark clarke in forum New To Java
    Replies: 1
    Last Post: 12-07-2011, 10:41 AM
  2. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 03:12 PM
  3. A question about using an iterator
    By TaxpayersMoney in forum New To Java
    Replies: 8
    Last Post: 05-21-2010, 09:18 PM
  4. Replies: 0
    Last Post: 05-18-2010, 01:53 AM
  5. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 02:32 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
  •