for loop:
List<String> list = new ArrayList<String>();
String[] arr = new String[]{"a","b","ac,"ad"};
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i));
}
foreach loop:
String[] arr = new String[]{"aaa","bbb","ccc"};
List<String> list = new ArrayList<String>();
list.add("d");
list.add("e");
for(String str : arr){
System.out.println(str);
}
for (String item : list) {
System.out.println(item);
}
iterator loop:
Iterator<String> it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
Performance comparison
Performance is a way of considering our selection of technical means, and look at the speed comparison of these three traversal methods
List<Long> list = new ArrayList<Long>();
long loop = 1000000;
for(long i = 0;i < loop;i++){
list.add(i);
}
// for循环
long startTime = System.currentTimeMillis();
for(int i = 0;i < list.size();i++){
}
long endTime = System.currentTimeMillis();
System.out.println("for循环:"+(endTime - startTime) + "ms\n");
// foreach 循环
startTime = System.currentTimeMillis();
for(Long lon : list){
}
endTime = System.currentTimeMillis();
System.out.println("foreach 循环:"+(endTime - startTime) + "ms\n");
// iterator 循环
startTime = System.currentTimeMillis();
Iterator<Long> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
}
endTime = System.currentTimeMillis();
System.out.println("iterator 循环:"+(endTime - startTime) + "ms");
The results show that:
difference:
1. Form difference
The form of for:
for(int i=0;i<arr.size();i++){...}
The form of foreach:
for(int i:arr){...}
The form of iterator:
Iterator it = arr.iterator()
while(it.hasNext()){ object o =it.next(); ...}
2. Condition difference
For needs to know the size of the collection or array, and it needs to be ordered, otherwise it cannot be traversed;
Neither foreach nor iterator need to know the size of the collection or array, they both get each element in the collection and then process it;
3. Polymorphic differences
Both for and foreach need to know the type of the collection, even the type of the elements in the collection, that is, they need to access the internal members;
Iterator is an interface type, does not care about the type of collection or array, and it can modify and delete the elements of the collection at any time.
public void display(Iterator<object> it){
while(it.hasNext()){
System.out.print(it.next()+"")
When we need to traverse different collections, we only need to pass the iterator of the collection (such as arr.iterator()) to understand. This is the advantage of iterator. It does not contain any type information about the sequence it traverses. The operation of traversing the sequence is separated from the underlying structure of the sequence. Iterators unify access to containers. This is also the best manifestation of interface decoupling.
4. Usage difference
The for loop is generally used to deal with relatively simple ordered collections or arrays of predictable size
foreach can be used to traverse any collection or array, and the operation is simple and easy to understand. His only downside is that he needs to understand the internal type of the collection
Iterator is the most powerful, it can modify or delete the elements inside the collection at any time, and it is done without knowing the type of the element and the collection (for the reason, please refer to the third point: polymorphic difference), when you need When implementing the same traversal method for different containers, iterators are the best choice!