/* Programmer : Matiga, Reuben D.
Program name : Iterators
Date Started : March 2,2009
Date Finished : March 3,2009
Purpose : Iterate through elements Java ArrayList using Iterator
Instructor : Dony Dongiapon
Subject : IT134
*/
package collection;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorDemo {
public static void main(String arg[])
{
ArrayList reu=new ArrayList();
for(int i=0;i<10;i++)
{
reu.add(new Integer(i));
}
System.out.println(reu); // 0,1,2,3,……9
Iterator itr=reu.iterator();
while(itr.hasNext())
{
Integer i=(Integer)itr.next();
if(i.intValue() % 2==0)
{
System.out.println(i);
}
else
{
itr.remove();
}
}
}
}
output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
2
4
6
8
Source: http://www.javaexamples4u.com/2009/02/iterator-source-code.html
I learned that iterator is an object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data is available a StopIteration exception is raised.
***********************************************************************
Wednesday, March 4, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment