what is iterator ??
is it a interface or object ??
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.
what is iterator ??
is it a interface or object ??
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!
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!
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!
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!
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();*
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!