实体Person类
public class Person { private String a; private int b; public Person(String a, int b) { super(); this.a = a; this.b = b; } public String getA() { return a; } public void setA(String a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } }
测试ArrayList的主程序
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class TestArrayList { private static final int COUNT = 800000; private static List<Person> persons = new ArrayList<Person>(); public static void main(String[] args) { init(); System.out.println("for循环测试结果:"+testFor()); System.out.println("foreach循环测试结果:"+testForeach()); System.out.println("iterator循环测试结果:"+testIterator()); } //初始化集合 public static void init(){ for (int i = 0; i <COUNT; i++) { persons.add(new Person("第"+i+"个元素",i)); } } //for循环便利集合 public static long testFor(){ long start = System.nanoTime(); Person p = null; for (int i = 0; i < persons.size(); i++) { p = persons.get(i); } return (System.nanoTime()-start)/1000; } //foreach循环便利集合 public static long testForeach(){ long start = System.nanoTime(); Person p = null; for (Person person : persons) { p = person; } return (System.nanoTime()-start)/1000; } //iterator循环便利集合 public static long testIterator(){ long start = System.nanoTime(); Person p = null; Iterator<Person> itreator = persons.iterator(); while(itreator.hasNext()){ p = itreator.next(); } return (System.nanoTime()-start)/1000; } }
 ArrayList测试结果
for循环测试结果:11145foreach循环测试结果:19421iterator循环测试结果:10748
测试LinkedList的主程序
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class TestArrayList { private static final int COUNT = 8000; private static List<Person> persons = new LinkedList<Person>(); public static void main(String[] args) { init(); System.out.println("for循环测试结果:"+testFor()); System.out.println("foreach循环测试结果:"+testForeach()); System.out.println("iterator循环测试结果:"+testIterator()); } //初始化集合 public static void init(){ for (int i = 0; i <COUNT; i++) { persons.add(new Person("第"+i+"个元素",i)); } } //for循环便利集合 public static long testFor(){ long start = System.nanoTime(); Person p = null; for (int i = 0; i < persons.size(); i++) { p = persons.get(i); } return (System.nanoTime()-start)/1000; } //foreach循环便利集合 public static long testForeach(){ long start = System.nanoTime(); Person p = null; for (Person person : persons) { p = person; } return (System.nanoTime()-start)/1000; } //iterator循环便利集合 public static long testIterator(){ long start = System.nanoTime(); Person p = null; Iterator<Person> itreator = persons.iterator(); while(itreator.hasNext()){ p = itreator.next(); } return (System.nanoTime()-start)/1000; } }
LinkedList测试结果
for循环测试结果:111336foreach循环测试结果:2736iterator循环测试结果:1830
记录的存取方式有两种

一种是顺序存储(数组,ArrayList..)可以根据其下标找到对应的记录

另一种是链接存储(LinkedList..)链接存储(拿单链表为例)则必须找到其前一个记录的位置才能够找到本记录。

 总结

for循环便于访问顺序存储的记录,而foreach和迭代器便于访问链接存储。

 

版权声明:本文转载自http://blog.csdn.net/c1347623993/article/details/9209767