what is the difference between iterator and lops
An Iterator is an Object, which goes through a Collection (e.g. Vector, List, ...) and you can do something with whatever the Iterator is pointing at.
A Loop is a construct, that repeats something for a certain amount of times.
Iterators and Loops are often used together. What I guess you mean is: What's the difference between
-
Vector<String> vector = new Vector<String>();
-
vector.add("Hello");
-
vector.add("World");
-
-
for(int i=0; i<vector.size();i++)
-
{
-
System.out.println(vector.elementAt(i));
-
}
-
and
-
Vector<String> vector = new Vector<String>();
-
vector.add("Hello");
-
vector.add("World");
-
-
Iterator vectorIterator = vector.iterator();
-
while(vectorIterator.hasNext())
-
{
-
System.out.println(vectorIterator.next());
-
}
-
(You can replace the Vector with another Collection.)
One big advantage of the first option is, that you always know, at which position you are.
One big advantage of the second option is, that you don't have to know the size of your Collection.
I can't think of any further differences in the usage at the moment, but the concept is, as you see, quite different.
Greetings,
Nepomuk