日期:2014-05-20 浏览次数:20698 次
import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; public class TimeTest { public static void main(String[] args) { int[] a = new int[999999]; ArrayList<Integer> b = new ArrayList<Integer>(); for(int i = 0 ; i < 999999;i++){ a[i] = i; b.add(i); } Calendar c = new GregorianCalendar(); long time1 = c.getTimeInMillis(); for(int i = 0 ; i< 999999 ; i++){ if(a[i] == 999998){ break; } } c = new GregorianCalendar(); long time2 = c.getTimeInMillis(); System.out.println("数组查找到999998,所消耗的时间:" + (time2 - time1)); c = new GregorianCalendar(); time1 = c.getTimeInMillis(); for(Integer num : b){ if(num == 999998){ break; } } c = new GregorianCalendar(); time2 = c.getTimeInMillis(); System.out.println("ArrayList查找到999998,所消耗的时间:" + (time2 - time1)); } }