I have to iterate with a for-loop
, but I want to know which way is better.
1:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0,size=tmpList.size(); i < size; i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
2:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0;i < tmpList.size(); i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
In the above, both for-loops
have the same content but just a difference in the loop condition. Can anyone tell me what is actually happening in the above iterations?