ArrayList与LinkList

℡╲_俬逩灬. 提交于 2020-01-23 16:07:18

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!