package cn.edu.bjut.chapter6; import java.util.ArrayList; import java.util.List; import java.util.Random; public class ArrayListConstructorTester { public static String getRandomString(int length) { String base = "abcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(length); for (int i = 0; i < length; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } public static void main(String[] args) { int n = 1000000; long startTime1 = System.currentTimeMillis(); List list1 = new ArrayList(); for (int i = 0; i < n; i++) { list1.add(getRandomString(5)); } long endTime1 = System.currentTimeMillis(); long startTime2 = System.currentTimeMillis(); List list2 = new ArrayList(n); for (int i = 0; i < n; i++) { list2.add(getRandomString(5)); } long endTime2 = System.currentTimeMillis(); System.out.println((endTime1 - startTime1) + "ms\t" + (endTime2 - startTime2) + "ms"); } }