67,513
社区成员
发帖
与我相关
我的任务
分享
public class LinkList_ArrayList {
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
getRemoveTime(500000,490000,250000);//当element越靠后remove OBJ ArrayList效率比LinkedList越明显,小的时候相差无极
}
public static void getRemoveTime(int count,int element,int index){
ArrayList<String> arr = new ArrayList<String>();//默认长度为10
LinkedList<String> linl = new LinkedList<String>();
ArrayList<String> con = new ArrayList<String>();
for(int i=1;i<=count;i++){
arr.add(i+"");//当数组空间已满,增加原有长度的1/2
}
for(int i=1;i<=count;i++){
linl.add(i+"");
}
for(int i=1;i<=10;i++){
int p = 200000;
con.add((p+i)+"");//当数组空间已满,增加原有长度的1/2
}
Long start_remove = new Date().getTime();
arr.remove(index);
Long end_remove = new Date().getTime();
System.out.println("ArrayList方法remove 在"+count+"条数据中,第"+index+"个位置需删除元素要耗时"+(end_remove-start_remove));
Long startlinl_remove = new Date().getTime();
linl.remove(index);
Long endlinl_remove = new Date().getTime();
System.out.println("LinkedList方法remove 在"+count+"条数据中,第"+index+"个位置需删除元素要耗时"+(endlinl_remove-startlinl_remove));
Long start_remove_OBJ = new Date().getTime();
arr.remove(element+"");
Long end_remove_OBJ = new Date().getTime();
System.out.println("ArrayList方法remove OBJ 在"+count+"条数据中,删除"+element+"元素要耗时"+(end_remove_OBJ-start_remove_OBJ));
Long startlinl_remove_OBJ = new Date().getTime();
linl.remove(element+"");
Long endlinl_remove_OBJ = new Date().getTime();
System.out.println("LinkedList方法remove OBJ 在"+count+"条数据中,删除"+element+"元素要耗时"+(endlinl_remove_OBJ-startlinl_remove_OBJ));
Long start_removeAll = new Date().getTime();
arr.removeAll(con);
Long end_removeAll = new Date().getTime();
System.out.println("ArrayList方法removeAll 在"+count+"条数据中,删除容器元素要耗时"+(end_removeAll-start_removeAll));
Long startlinl_removeAll = new Date().getTime();
linl.removeAll(con);
Long endlinl_removeAll = new Date().getTime();
System.out.println("LinkedList方法removeAll 在"+count+"条数据中,删除容器元素要耗时"+(endlinl_removeAll-startlinl_removeAll));
}
public static void getIndexOfTime(int count,int count_con,int index){
ArrayList<String> arr = new ArrayList<String>();//默认长度为10
LinkedList<String> linl = new LinkedList<String>();
ArrayList<String> con = new ArrayList<String>();
for(int i=1;i<=count;i++){
arr.add(i+"");//当数组空间已满,增加原有长度的1/2
}
for(int i=1;i<=count;i++){
linl.add(i+"");//当数组空间已满,增加原有长度的1/2
}
for(int i=1;i<=count_con;i++){
int p = 400000;
con.add((p+i)+"");//当数组空间已满,增加原有长度的1/2
}
Long start_indexof = new Date().getTime();
arr.indexOf(index+"");
Long end_indexof = new Date().getTime();
System.out.println("ArrayList方法indexOf 在"+count+"条数据中,第"+index+"个位置需要耗时"+(end_indexof-start_indexof));
Long startlinl_indexof = new Date().getTime();
linl.indexOf(index+"");
Long endlinl_indexof = new Date().getTime();
System.out.println("LinkedList方法indexOf 在"+count+"条数据中,第"+index+"个位置需要耗时"+(endlinl_indexof-startlinl_indexof));
Long start_contains = new Date().getTime();
arr.contains(index+"");
Long end_contains = new Date().getTime();
System.out.println("ArrayList方法contains 在"+count+"条数据中,第"+index+"个位置需要耗时"+(end_contains-start_contains));
Long startlinl_contains = new Date().getTime();
linl.contains(index+"");
Long endlinl_contains = new Date().getTime();
System.out.println("LinkedList方法contains 在"+count+"条数据中,第"+index+"个位置需要耗时"+(endlinl_contains-startlinl_contains));
Long start_containsAll = new Date().getTime();
arr.containsAll(con);
Long end_containsAll = new Date().getTime();
System.out.println("ArrayList方法containsAll 在"+count+"条数据中,第"+index+"个位置需要耗时"+(end_containsAll-start_containsAll));
Long startlinl_containsAll = new Date().getTime();
linl.containsAll(con);
Long endlinl_containsAll = new Date().getTime();
System.out.println("LinkedList方法containsAll 在"+count+"条数据中,第"+index+"个位置需要耗时"+(endlinl_containsAll-startlinl_containsAll));
}
}
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
private E remove(Entry<E> e) {
if (e == header)
throw new NoSuchElementException();
E result = e.element;
e.previous.next = e.next;
e.next.previous = e.previous;
e.next = e.previous = null;
e.element = null;
size--;
modCount++;
return result;
}