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

Thread: Iterator

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Iterator

    what is iterator ??
    is it a interface or object ??


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Iterator

    The API is your best friend. This is the first result for googling "Java iterator": Iterator (Java Platform SE 7 )

    According to the API, it's an interface. But asking whether something is an interface or an object seems to miss the point: and object can be an interface.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterator

    Quote Originally Posted by KevinWorkman View Post
    The API is your best friend. This is the first result for googling "Java iterator": Iterator (Java Platform SE 7 )

    According to the API, it's an interface. But asking whether something is an interface or an object seems to miss the point: and object can be an interface.
    i m new to java and as a matter of fact ,i m a bit confusing that how a interface contain whole methods..i have read that interface can contain only the defination of the method ..can you make me understand about this one ???

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Iterator

    Here is an example:

    List is an interface. The ArrayList class is an implementation of that interface. Since an ArrayList *is a* List, you can have code like this:

    List myList = new ArrayList();

    The List interface only has method signatures, not their bodies. However, since myList is an ArrayList, I can still do things like this:

    myList.add("test");

    A similar thing happens with the Iterator interface.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterator

    Quote Originally Posted by KevinWorkman View Post
    Here is an example:

    List is an interface. The ArrayList class is an implementation of that interface. Since an ArrayList *is a* List, you can have code like this:

    List myList = new ArrayList();

    The List interface only has method signatures, not their bodies. However, since myList is an ArrayList, I can still do things like this:

    myList.add("test");

    A similar thing happens with the Iterator interface.
    Well thank you . But one thing more in case of List interface ,class ArrayList is implementing List interface but my question is which class is implementing the Interface Iterator .

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Iterator

    Quote Originally Posted by gautammuktsar@gmail.com View Post
    Well thank you . But one thing more in case of List interface ,class ArrayList is implementing List interface but my question is which class is implementing the Interface Iterator .
    That depends on how you're getting the interface. It could be any number of classes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterator

    Quote Originally Posted by KevinWorkman View Post
    That depends on how you're getting the interface. It could be any number of classes.
    then could be necessary for the class which implements the Iterator to provide defination for their all 3 methods ??...but while using Iterator to transverse the elements of collection(container) we are not defining any of the methods..please give a short example

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Iterator

    Quote Originally Posted by gautammuktsar@gmail.com View Post
    then could be necessary for the class which implements the Iterator to provide defination for their all 3 methods ??...but while using Iterator to transverse the elements of collection(container) we are not defining any of the methods..please give a short example
    No, *you* give a short example. How are you getting your Iterator? What does the function that returns the Iterator actually do? You can access the source code of any Java method via the src.zip file in your JDK directory.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Iterator

    Quote Originally Posted by KevinWorkman View Post
    No, *you* give a short example. How are you getting your Iterator? What does the function that returns the Iterator actually do? You can access the source code of any Java method via the src.zip file in your JDK directory.
    import java.util.*;
     
    class CollectionEx{
     
    	public static void main(String arg[]){
    		Vector obj = new Vector();
     
    		obj.add(new Integer(99));
    		obj.add(new Integer(78));
    		obj.add(66);
    		obj.add(56.89F);
    		obj.add("Hello");
     
    		Iterator it = obj.iterator();
    		while (it.hasNext()){
    			System.out.println(it.next());
    		}
    	}
    }

    I am not getting that how this works *Iterator it = obj.iterator();*

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Iterator

    Like I said, your best bet is to look at the source for the Vector.iterator() function. It looks like this:

     /**
         * Returns an iterator over the elements in this list in proper sequence.
         *
         * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
         *
         * @return an iterator over the elements in this list in proper sequence
         */
        public synchronized Iterator<E> iterator() {
            return new Itr();
        }

    And the Itr class is an inner class inside the Vector class. It looks like this:

        /**
         * An optimized version of AbstractList.Itr
         */
        private class Itr implements Iterator<E> {
            int cursor;       // index of next element to return
            int lastRet = -1; // index of last element returned; -1 if no such
            int expectedModCount = modCount;
     
            public boolean hasNext() {
                // Racy but within spec, since modifications are checked
                // within or after synchronization in next/previous
                return cursor != elementCount;
            }
     
            public E next() {
                synchronized (Vector.this) {
                    checkForComodification();
                    int i = cursor;
                    if (i >= elementCount)
                        throw new NoSuchElementException();
                    cursor = i + 1;
                    return elementData(lastRet = i);
                }
            }
     
            public void remove() {
                if (lastRet == -1)
                    throw new IllegalStateException();
                synchronized (Vector.this) {
                    checkForComodification();
                    Vector.this.remove(lastRet);
                    expectedModCount = modCount;
                }
                cursor = lastRet;
                lastRet = -1;
            }
     
            final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
            }
        }

    But all you really need to know is that the iterator() function returns and implementation of the Iterator interface. An interface is simply a way of guaranteeing which functions you know you have. You know you have the functions declared by the Iterator interface in whatever object is returned by the iterator() function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Iterator problems
    By xveilsidex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2013, 03:10 PM
  2. Sorting using Iterator
    By palaboui in forum Collections and Generics
    Replies: 1
    Last Post: November 15th, 2012, 02:59 PM
  3. Help With Iterator code... please help me..
    By basketball8533 in forum Collections and Generics
    Replies: 0
    Last Post: November 20th, 2011, 11:44 PM
  4. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 09:51 PM
  5. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM