What is difference between Iterator and for loop

Answer Posted / amit r

There are basic 3 differences between For and Iterater.
1.
Using Iterater we can check if the object exists or not by using hasNext method. Where as in For Loop, there is not such method. Therefore, For Loop will be executed at fixed amount at every time.
2.
Using Iterator we can add or remove objects from the underlying collection. In For Loop, if we do so, concurrent modification exception will be thrown.
3.
Using Iterator we can move forward or backward where as For Loop can't.
:)

Is This Answer Correct ?    7 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we declare main () method as non static?

524


What is the do while loop syntax?

556


what is comparable and comparator interface?

582


What is an empirical question?

544


Can we sort hashset in java?

631






How to make a class or a bean serializable?

542


What is the purpose of javac exe?

562


What data type is string java?

541


Does it matter in what order catch statements for filenotfoundexception and ioexception are written?

510


Explain the difference between abstract class and interface in java?

541


What is a pattern what is an anti pattern?

512


How the interruptible method gets implemented?

569


From the two, which would be easier to write: synchronization code for ten threads or two threads?

619


Give the difference between the println method and sqrt method?

626


write a program that list all permutations of ABCDEF in which A appears before B?

2015