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
public class Test { public static void main(String[] args) { int countA = 0; int countB = 0; int countC = 0; int countD = 0; int countE = 0; int countF = 0; int countG = 0; int countH = 0; int countI = 0; int countJ = 0; int countK = 0; int countL = 0; int countM = 0; int countN = 0; int countO = 0; int countP = 0; int countQ = 0; int countR = 0; int countS = 0; int countT = 0; int countU = 0; int countV = 0; int countW = 0; int countX = 0; int countY = 0; int countZ = 0; } } Can anybody tell me any alternate solution(like loop or something) to automate this initialization process. Ex:- for(char chr='A';chr<='Z'; chr++) { (int) String "count"+chr = 0; }
Explain polymorphism citing an example.
Write a program to find maximum and minimum number in array?
How do you convert an int to a string in java?
what are synchronized methods and synchronized statements? : Java thread
What is null mean in java?
What are aggregate functions explain with examples?
What ide should I use for java?
Write a program to print 15 random numbers using foreach of java 8?
What is unmodifiable collection in java?
what is synchronization and why is it important? : Java thread
When can you say a graph to be a tree?
What are the two basic ways in which classes that can be run as threads may be defined?
What is java Applet?
What is the purpose of garbage collection in java?