1
ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构;
2
LinkedList是更快的
比较代码:
public class LinkedListDemo2 {
public static void main(String []args) {
test1();
//test2();打开test1()关闭test2()
}
public static void test1(){
long l=System.currentTimeMillis();//系统当前毫秒数
try{
List<byte[]>list=new LinkedList<byte[]>();
boolean b=true;
while(b) {
list.add(new byte[100*1024]);//会导致堆溢出
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
System.out.println(System.currentTimeMillis()-l);
//后来的毫秒数减去之前的就是运行时间
}
}
public static void test2(){
long l=System.currentTimeMillis();//系统当前毫秒数
try{
List<byte[]>list=new ArrayList<byte[]>();
boolean b=true;
int i=0;
while(b) {
list.add(new byte[100*1024]);//会导致堆溢出
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
System.out.println(System.currentTimeMillis()-l);
}
}
}
test1()结果:LinkList
861
test2()结果:ArrayList
915