JavaList与管理是Java的核心技术之一

上传人:M****1 文档编号:512200154 上传时间:2022-08-04 格式:DOCX 页数:7 大小:17.58KB
返回 下载 相关 举报
JavaList与管理是Java的核心技术之一_第1页
第1页 / 共7页
JavaList与管理是Java的核心技术之一_第2页
第2页 / 共7页
JavaList与管理是Java的核心技术之一_第3页
第3页 / 共7页
JavaList与管理是Java的核心技术之一_第4页
第4页 / 共7页
JavaList与管理是Java的核心技术之一_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《JavaList与管理是Java的核心技术之一》由会员分享,可在线阅读,更多相关《JavaList与管理是Java的核心技术之一(7页珍藏版)》请在金锄头文库上搜索。

1、深入Java集合学习系列:ArrayList的实现原理1. ArrayList概述: ArrayList是List接口的可变数组的实现。实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。 每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。它总是至少等于列表的大小。随着向ArrayList中不断添加元 素,其容量也自动增长。自动增长会带来数据向新数组的重新拷贝,因此,如果可预知数据量的多少,可在构造ArrayList时指定其容量。在添加大量元素 前,应用程序也可以使用ens

2、ureCapacity操作来增加ArrayList实例的容量,这可以减少递增式再分配的数量。 注意,此实现不是同步的。如果多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。2. ArrayList的实现: 对于ArrayList而言,它实现List接口、底层使用数组保存所有元素。其操作基本上是对数组的操作。下面我们来分析ArrayList的源代码: 1) 底层使用数组实现:Java代码 1. privatetransientObjectelementData; 2) 构造方法: ArrayList提供了三种方式的构造器,可以构造一个默认初

3、始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。Java代码 1. publicArrayList()2. this(10);3. 4. 5. publicArrayList(intinitialCapacity)6. super();7. if(initialCapacity0)8. thrownewIllegalArgumentException(IllegalCapacity:+initialCapacity);9. this.elementData=newObject

4、initialCapacity;10. 11. 12. publicArrayList(Collectionc)13. elementData=c.toArray();14. size=elementData.length;15. /c.toArraymight(incorrectly)notreturnObject(see6260652)16. if(elementData.getClass()!=Object.class)17. elementData=Arrays.copyOf(elementData,size,Object.class);18. 3) 存储: ArrayList提供了s

5、et(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection c)、addAll(int index, Collection c)这些添加元素的方法。下面我们一一讲解:Java代码 1. /用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。2. publicEset(intindex,Eelement)3. RangeCheck(index);4. 5. EoldValue=(E)elementDataindex;6. elementDataindex=element;7. re

6、turnoldValue;8. Java代码 1. /将指定的元素添加到此列表的尾部。2. publicbooleanadd(Ee)3. ensureCapacity(size+1);4. elementDatasize+=e;5. returntrue;6. Java代码 1. /将指定的元素插入此列表中的指定位置。2. /如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。3. publicvoidadd(intindex,Eelement)4. if(indexsize|index0)5. thrownewIndexOutOfBoundsException

7、(Index:+index+,Size:+size);6. /如果数组长度不足,将进行扩容。7. ensureCapacity(size+1);/IncrementsmodCount!8. /将elementData中从Index位置开始、长度为size-index的元素,9. /拷贝到从下标为index+1位置开始的新的elementData数组中。10. /即将当前位于该位置的元素以及所有后续元素右移一个位置。11. System.arraycopy(elementData,index,elementData,index+1,size-index);12. elementDataindex

8、=element;13. size+;14. Java代码 1. /按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。2. publicbooleanaddAll(Collectionc)3. Objecta=c.toArray();4. intnumNew=a.length;5. ensureCapacity(size+numNew);/IncrementsmodCount6. System.arraycopy(a,0,elementData,size,numNew);7. size+=numNew;8. returnnumNew!

9、=0;9. Java代码 1. /从指定的位置开始,将指定collection中的所有元素插入到此列表中。2. publicbooleanaddAll(intindex,Collectionc)3. if(indexsize|index0)13. System.arraycopy(elementData,index,elementData,index+numNew,numMoved);14. 15. System.arraycopy(a,0,elementData,index,numNew);16. size+=numNew;17. returnnumNew!=0;18. 4) 读取:Java

10、代码 1. /返回此列表中指定位置上的元素。2. publicEget(intindex)3. RangeCheck(index);4. 5. return(E)elementDataindex;6. 5) 删除: ArrayList提供了根据下标或者指定对象两种方式的删除功能。如下:Java代码 1. /移除此列表中指定位置上的元素。2. publicEremove(intindex)3. RangeCheck(index);4. 5. modCount+;6. EoldValue=(E)elementDataindex;7. 8. intnumMoved=size-index-1;9. i

11、f(numMoved0)10. System.arraycopy(elementData,index+1,elementData,index,numMoved);11. elementData-size=null;/Letgcdoitswork12. 13. returnoldValue;14. Java代码 1. /移除此列表中首次出现的指定元素(如果存在)。这是应为ArrayList中允许存放重复的元素。2. publicbooleanremove(Objecto)3. /由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。4. if(o=null)5. for(intindex=0;indexsize;index+)6. if(elementDataindex=null)7. /类似remove(intindex),移除列表中指定位置上的元素。8. fastRemove(index);9. returntrue;10. 11. else12. for(intindex=0;indexsize;index+)13.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号