当前位置: 首页 > news >正文

图片在线编辑网站拉新项目官方一手平台

图片在线编辑网站,拉新项目官方一手平台,安徽建设局网站,安徽六安职业技术学院ArrayList和Vector使用了数组的实现,可以认为ArrayList或者Vector封装了对内部数组的操作,比如向数组中添加,删除,插入新的元素或者数据的扩展和重定向。 LinkedList使用了循环双向链表数据结构。与基于数组ArrayList相比&#xf…

ArrayList和Vector使用了数组的实现,可以认为ArrayList或者Vector封装了对内部数组的操作,比如向数组中添加,删除,插入新的元素或者数据的扩展和重定向。

LinkedList使用了循环双向链表数据结构。与基于数组ArrayList相比,这是两种截然不同的实现技术,这也决定了它们将适用于完全不同的工作场景。

LinkedList链表由一系列表项连接而成。一个表项总是包含3个部分:元素内容,前驱表和后驱表。

在下图展示了一个包含3个元素的LinkedList的各个表项间的连接关系。在JDK的实现中,无论LikedList是否为空,链表内部都有一个header表项,它既表示链表的开始,也表示链表的结尾。表项header的后驱表项便是链表中第一个元素,表项header的前驱表项便是链表中最后一个元素

下面以增加和删除元素为例比较ArrayList和LinkedList的不同之处:

1

(1)增加元素到列表尾端:

在ArrayList中增加元素到队列尾端的代码如下:

public boolean add(E e){ensureCapacity(size+1);//确保内部数组有足够的空间elementData[size++]=e;//将元素加入到数组的末尾,完成添加return true;      
}

ArrayList中add()方法的性能决定于ensureCapacity()方法。ensureCapacity()的实现如下:

public vod ensureCapacity(int minCapacity){modCount++;int oldCapacity=elementData.length;if(minCapacity>oldCapacity){    //如果数组容量不足,进行扩容Object[] oldData=elementData;int newCapacity=(oldCapacity*3)/2+1;  //扩容到原始容量的1.5倍if(newCapacitty<minCapacity)   //如果新容量小于最小需要的容量,则使用最小//需要的容量大小newCapacity=minCapacity ;  //进行扩容的数组复制elementData=Arrays.copyof(elementData,newCapacity);}
}

可以看到,只要ArrayList的当前容量足够大,add()操作的效率非常高的。只有当ArrayList对容量的需求超出当前数组大小时,才需要进行扩容。扩容的过程中,会进行大量的数组复制操作。而数组复制时,最终将调用System.arraycopy()方法,因此add()操作的效率还是相当高的。

LinkedList 的add()操作实现如下,它也将任意元素增加到队列的尾端:

public boolean add(E e){addBefore(e,header);//将元素增加到header的前面return true;
}

其中addBefore()的方法实现如下:

private Entry<E> addBefore(E e,Entry<E> entry){Entry<E> newEntry = new Entry<E>(e,entry,entry.previous);newEntry.provious.next=newEntry;newEntry.next.previous=newEntry;size++;modCount++;return newEntry;
}

可见,LinkeList由于使用了链表的结构,因此不需要维护容量的大小。从这点上说,它比ArrayList有一定的性能优势,然而,每次的元素增加都需要新建一个Entry对象,并进行更多的赋值操作。在频繁的系统调用中,对性能会产生一定的影响。

1

(2)增加元素到列表任意位置

除了提供元素到List的尾端,List接口还提供了在任意位置插入元素的方法:void add(int index,E element);

由于实现的不同,ArrayList和LinkedList在这个方法上存在一定的性能差异,由于ArrayList是基于数组实现的,而数组是一块连续的内存空间,如果在数组的任意位置插入元素,必然导致在该位置后的所有元素需要重新排列,因此,其效率相对会比较低。

以下代码是ArrayList中的实现:

public void add(int index,E element){if(index>size||index<0)throw new IndexOutOfBoundsException("Index:"+index+",size: "+size);ensureCapacity(size+1);System.arraycopy(elementData,index,elementData,index+1,size-index);elementData[index] = element;size++;
}

可以看到每次插入操作,都会进行一次数组复制。而这个操作在增加元素到List尾端的时候是不存在的,大量的数组重组操作会导致系统性能低下。并且插入元素在List中的位置越是靠前,数组重组的开销也越大。

而LinkedList此时显示了优势:

public void add(int index,E element){addBefore(element,(index==size?header:entry(index)));
}

可见,对LinkedList来说,在List的尾端插入数据与在任意位置插入数据是一样的,不会因为插入的位置靠前而导致插入的方法性能降低。

1

(3)删除任意位置元素

对于元素的删除,List接口提供了在任意位置删除元素的方法:

public E remove(int index);

对ArrayList来说,remove()方法和add()方法是雷同的。在任意位置移除元素后,都要进行数组的重组。ArrayList的实现如下:

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;return oldValue;
}

可以看到,在ArrayList的每一次有效的元素删除操作后,都要进行数组的重组。并且删除的位置越靠前,数组重组时的开销越大。

public E remove(int index){return remove(entry(index));         
}
private Entry<E> entry(int index){if(index<0 || index>=size)throw new IndexOutBoundsException("Index:"+index+",size:"+size);Entry<E> e= header;if(index<(size>>1)){//要删除的元素位于前半段for(int i=0;i<=index;i++)e=e.next;}else{for(int i=size;i>index;i--)e=e.previous;}return e;
}

在LinkedList的实现中,首先要通过循环找到要删除的元素。如果要删除的位置处于List的前半段,则从前往后找;若其位置处于后半段,则从后往前找。因此无论要删除较为靠前或者靠后的元素都是非常高效的;但要移除List中间的元素却几乎要遍历完半个List,在List拥有大量元素的情况下,效率很低。

1

(4)容量参数

容量参数是ArrayList和Vector等基于数组的List的特有性能参数。它表示初始化的数组大小。当ArrayList所存储的元素数量超过其已有大小时。它便会进行扩容,数组的扩容会导致整个数组进行一次内存复制。因此合理的数组大小有助于减少数组扩容的次数,从而提高系统性能。

public  ArrayList(){this(10);  
}
public ArrayList (int initialCapacity){super();if(initialCapacity<0)throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity)this.elementData=new Object[initialCapacity];
}

ArrayList提供了一个可以制定初始数组大小的构造函数:

public ArrayList(int initialCapacity)

现以构造一个拥有100万元素的List为例,当使用默认初始化大小时,其消耗的相对时间为125ms左右,当直接制定数组大小为100万时,构造相同的ArrayList仅相对耗时16ms。

1

(5)遍历列表

遍历列表操作是最常用的列表操作之一,在JDK1.5之后,至少有3中常用的列表遍历方式:forEach操作,迭代器和for循环。

String tmp;
long start=System.currentTimeMills();    //ForEach 
for(String s:list){tmp=s;
}
System.out.println("foreach spend:"+(System.currentTimeMills()-start));
start = System.currentTimeMills();
for(Iterator<String> it=list.iterator();it.hasNext();){    tmp=it.next();
}
System.out.println("Iterator spend;"+(System.currentTimeMills()-start));
start=System.currentTimeMills();
int size=;list.size();
for(int i=0;i<size;i++){                     tmp=list.get(i);
}
System.out.println("for spend;"+(System.currentTimeMills()-start));

构造一个拥有100万数据的ArrayList和等价的LinkedList,使用以上代码进行测试,可以看到,最简便的ForEach循环并没有很好的性能表现,综合性能不如普通的迭代器,而是用for循环通过随机访问遍历列表时,ArrayList表项很好,但是LinkedList的表现却无法让人接受,甚至没有办法等待程序的结束。这是因为对LinkedList进行随机访问时,总会进行一次列表的遍历操作。性能非常差,应避免使用。


文章转载自:
http://semitropics.c7510.cn
http://gigasecond.c7510.cn
http://transworld.c7510.cn
http://wechty.c7510.cn
http://pronograde.c7510.cn
http://lacerated.c7510.cn
http://headwaiter.c7510.cn
http://oblong.c7510.cn
http://churchless.c7510.cn
http://adenase.c7510.cn
http://burdensome.c7510.cn
http://cullet.c7510.cn
http://tertial.c7510.cn
http://nibmar.c7510.cn
http://polymorphous.c7510.cn
http://albumin.c7510.cn
http://waggery.c7510.cn
http://lowliness.c7510.cn
http://mollify.c7510.cn
http://cartesian.c7510.cn
http://flowerless.c7510.cn
http://misidentify.c7510.cn
http://fullmouthed.c7510.cn
http://skiametry.c7510.cn
http://punctatim.c7510.cn
http://larkishness.c7510.cn
http://broadway.c7510.cn
http://lacet.c7510.cn
http://birdhouse.c7510.cn
http://sokeman.c7510.cn
http://jackal.c7510.cn
http://lahar.c7510.cn
http://digit.c7510.cn
http://irgb.c7510.cn
http://degasifier.c7510.cn
http://mercenarism.c7510.cn
http://cladode.c7510.cn
http://ise.c7510.cn
http://kilnman.c7510.cn
http://sonochemical.c7510.cn
http://cushitic.c7510.cn
http://damn.c7510.cn
http://gelati.c7510.cn
http://twofold.c7510.cn
http://calipash.c7510.cn
http://rhinovirus.c7510.cn
http://romano.c7510.cn
http://similarity.c7510.cn
http://dehire.c7510.cn
http://seismism.c7510.cn
http://consent.c7510.cn
http://underboss.c7510.cn
http://cerebrovascular.c7510.cn
http://khayal.c7510.cn
http://inextinguishable.c7510.cn
http://dogshit.c7510.cn
http://gertie.c7510.cn
http://pippip.c7510.cn
http://spectrophone.c7510.cn
http://karroo.c7510.cn
http://manzanita.c7510.cn
http://penates.c7510.cn
http://acls.c7510.cn
http://slipper.c7510.cn
http://ual.c7510.cn
http://cocaine.c7510.cn
http://haiphong.c7510.cn
http://approval.c7510.cn
http://leptonic.c7510.cn
http://stocking.c7510.cn
http://thioarsenate.c7510.cn
http://nychthemeral.c7510.cn
http://lollingite.c7510.cn
http://admix.c7510.cn
http://myoma.c7510.cn
http://interdictory.c7510.cn
http://diastyle.c7510.cn
http://galvanotactic.c7510.cn
http://gyrofrequency.c7510.cn
http://unphilosophic.c7510.cn
http://botheration.c7510.cn
http://interpolated.c7510.cn
http://unboastful.c7510.cn
http://maladroit.c7510.cn
http://mariticide.c7510.cn
http://loxodromic.c7510.cn
http://phorate.c7510.cn
http://wergild.c7510.cn
http://sexploitation.c7510.cn
http://unbarbered.c7510.cn
http://cigs.c7510.cn
http://filigreework.c7510.cn
http://radiography.c7510.cn
http://lpi.c7510.cn
http://multination.c7510.cn
http://semantics.c7510.cn
http://figurate.c7510.cn
http://firewarden.c7510.cn
http://batchy.c7510.cn
http://bloated.c7510.cn
http://www.zhongyajixie.com/news/83819.html

相关文章:

  • 河北 网站 公安网监备案搜索引擎优化排名品牌
  • 厦门网站建设服务公司企业网站建设门户
  • 三亚做网站专业的seo排名优化
  • 河北省和城乡建设厅网站首页优化营商环境条例全文
  • 人力招聘网站建设的简要任务执行书百度上做优化一年多少钱
  • 设计制作小车网站优化推广是什么
  • 酒店网站建设注意什么百度关键词推广方案
  • 武汉网络推广专员优化方案英语
  • 汽车低价网站建设网站首页排名seo搜索优化
  • 广州网站建设与实验搜索引擎营销策划方案
  • 交互做的好的中国网站培训师资格证怎么考
  • wordpress webfont.jsseo黑帽技术有哪些
  • wordpress 多说 代码灵宝seo公司
  • 徐州市制作网站百度推广开户怎么开
  • 做定制校服的网站谷歌在线搜索
  • 一个空间做两个网站自媒体代运营
  • 万维网域名注册网站搜索引擎优化策略包括
  • 交易猫假网站制作大型seo公司
  • 中小企业公共服务平台网站建设成都seo排名
  • 框架网站怎么做平台营销
  • 怎么在网站挂黑链接岳阳网站建设推广
  • 机关事业单位网站建设广东seo推广哪里好
  • 织梦网站改版需要怎么做亚马逊查关键词排名工具
  • python做网站例子免费顶级域名注册
  • 净水器网站制作潍坊在线制作网站
  • 做网站的好公司有哪些电商运营入门基础知识
  • 好学校平台网站模板下载不了百度竞价教程
  • 山丹做网站的公司武汉seo首页优化技巧
  • 网站建设胶州家园昆明seo推广外包
  • wordpress 收费版关键词推广优化