ArrayList适应于查询量大,修改频率较低的场景
LinkedList适应于增删改量较大的场景
代码展示:
ArrayList list1 = new ArrayList();
long l = System.currentTimeMillis();//起始时间
Random random = new Random();
for (int i = 0; i < 100000; i++) {
list1.add(random.nextInt(100000));//插入数据获取
}
long l1 = System.currentTimeMillis();//截止时间
long l11 = l1 - l;//运行时间
System.out.print("ArrayList插入数据的时间为" + l11 + " ");
long t = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {//获取数据
list1.get(i);
}
long t1 = System.currentTimeMillis();
long t11 = t1 - t;
System.out.println("ArrayList获取数据的时间为" + t11);
LinkedList list2 = new LinkedList();
long m = System.currentTimeMillis();
for (int j = 0; j < 100000; j++) {
list2.add(j);
}
long m1 = System.currentTimeMillis();
long m11 = m1 - m;
System.out.print("LinkedList插入数据的时间为" + m11 + " ");
long n = System.currentTimeMillis();
for (int j = 0; j < 100000; j++) {
list2.get(j);
}
long n1 = System.currentTimeMillis();
long n11= n1 - n;
System.out.println("LinkedList获取数据的时间为" + n11);
运行结果为:
ArrayList插入数据的时间为51 ArrayList获取数据的时间为0
LinkedList插入数据的时间为3 LinkedList获取数据的时间为10574