java arraylist范围_Java常见集合之ArrayList深入分析

1 /*

2 继承自AbstractList,实现了List、RandomAccess、Cloneable、Serializable接口3 1)RandomAccess接口:用来快速随机存取,在实现了该接口后,用普通for来遍历,性能更高4 2)Cloneable接口:实现了该接口,就可以使用Object.Clone()方法了5 3)Serializable接口:实现了该接口,表明该类可以被序列化6 */

7 public class ArrayList extends AbstractList

8 implements List, RandomAccess, Cloneable, java.io.Serializable9 {10 //版本号

11 private static final long serialVersionUID = 8683452581122892189L;12 //缺省容量

13 private static final int DEFAULT_CAPACITY = 10;14 //缺省空对象数组

15 private static final Object[] EMPTY_ELEMENTDATA ={};16 //默认大小的对象数组

17 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA ={};18 //元素数组

19 transientObject[] elementData;20 //数组的大小

21 private intsize;22

23 //构造方法24 //无参构造 Constructs an empty list with an initial capacity of ten

25 publicArrayList() {26 this.elementData =DEFAULTCAPACITY_EMPTY_ELEMENTDATA;27 }28

29 /**

30 * Constructs an empty list with the specified initial capacity.31 *32 *@paraminitialCapacity the initial capacity of the list33 *@throwsIllegalArgumentException if the specified initial capacity34 * is negative35 */

36 public ArrayList(intinitialCapacity) {37 if (initialCapacity > 0) {38 this.elementData = newObject[initialCapacity];39 } else if (initialCapacity == 0) {40 this.elementData =EMPTY_ELEMENTDATA;41 } else{42 throw new IllegalArgumentException("Illegal Capacity: "+

43 initialCapacity);44 }45 }46

47 /**

48 * Constructs a list containing the elements of the specified49 * collection, in the order they are returned by the collection's50 * iterator.51 *52 *@paramc the collection whose elements are to be placed into this list53 *@throwsNullPointerException if the specified collection is null54 */

55 public ArrayList(Collection extends E>c) {56 elementData = c.toArray(); //转换为数组

57 if ((size = elementData.length) != 0) { //数组长度大于058 //c.toArray might (incorrectly) not return Object[] (see 6260652)

59 if (elementData.getClass() != Object[].class)60 elementData = Arrays.copyOf(elementData, size, Object[].class);61 } else{62 //replace with empty array.

63 this.elementData =EMPTY_ELEMENTDATA;64 }65 }66 //总体而言:arrayList的构造方法就是做一件事,初始化一下储存数据的容器,67 //其实本质就是一个数组,在其中就叫elementData68

69 //核心方法

70 /**

71 * Appends the specified element to the end of this list.72 * 默认直接在末尾添加元素73 *74 *@parame element to be appended to this list75 *@returntrue (as specified by {@linkCollection#add})76 */

77 public boolean add(E e) { //elementData=[1,2,3] e=678 //确保数组的容量足够

79 ensureCapacityInternal(size + 1); //Increments modCount!!80 //数组尾部添加元素e

81 elementData[size++] =e;82 return true;83 }84

85 private void ensureCapacityInternal(int minCapacity) { //minCapacity=4

87 ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));88 }89

90 //计算容量

91 private static int calculateCapacity(Object[] elementData, int minCapacity) {//elementData=[1,2,3],minCapacity=492 //如果元素数组等于默认容量的元素数组,就返回DEFAULT_CAPACITY和minCapacity两者的最大值

93 if (elementData ==DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {94 returnMath.max(DEFAULT_CAPACITY, minCapacity);95 }96 //否则就返回minCapacity

97 return minCapacity; //4

98 }99

100 private void ensureExplicitCapacity(int minCapacity) { //minCapacity=4

101 modCount++;102

103 //overflow-conscious code

104 /*

105 这里有两种情况:106 情况一:elementData为空数组,minCapacity=size+1=1,1-0>0,调用grow()进行扩容107 情况二:elementData不为空数组,minCapacity=size+1,为elementData原始的数组(未调用add方法前),108 如果length不够用,再调用grow()进行扩容109 */

110 if (minCapacity - elementData.length > 0)111 grow(minCapacity);112 }113

114 /**

115 * 增加容量以确保它至少可以容纳由最小容量参数指定的元素数116 * Increases the capacity to ensure that it can hold at least the117 * number of elements specified by the minimum capacity argument.118 *119 *@paramminCapacity the desired minimum capacity120 */

121 private void grow(intminCapacity) {122 //overflow-conscious code123 //将原始的elementData.length赋给oldCapacity

124 int oldCapacity =elementData.length;125 //新容量=1.5*oldCapacity

126 int newCapacity = oldCapacity + (oldCapacity >> 1);127 //如果elementData为空数组,那么oldCapacity和newCapacity都为0,将newCapacity直接赋值为10

128 if (newCapacity - minCapacity < 0)129 newCapacity =minCapacity;130 //如果newCapacity超过了最大的容量限制,将调用hugeCapacity()做进一步处理

131 if (newCapacity - MAX_ARRAY_SIZE > 0)132 newCapacity =hugeCapacity(minCapacity);133 //minCapacity is usually close to size, so this is a win:134 //newCapacity确定好了以后,就copyof数组,进行扩容

135 elementData =Arrays.copyOf(elementData, newCapacity);136 }137

138 private static int hugeCapacity(intminCapacity) {139 if (minCapacity < 0) //overflow

140 throw newOutOfMemoryError();141 //判断minCapacity是否大于最大的数组容量MAX_ARRAY_SIZE:是,返回Integer.MAX_VALUE(2147483647);142 //否则返回MAX_ARRAY_SIZE

143 return (minCapacity > MAX_ARRAY_SIZE) ?

144 Integer.MAX_VALUE :145 MAX_ARRAY_SIZE;146 }147

148 /**

149 * 在指定位置插入元素150 * Inserts the specified element at the specified position in this151 * list. Shifts the element currently at that position (if any) and152 * any subsequent elements to the right (adds one to their indices).153 *154 *@paramindex index at which the specified element is to be inserted155 *@paramelement element to be inserted156 *@throwsIndexOutOfBoundsException {@inheritDoc}157 */

158 public void add(intindex, E element) {159 //检查插入的位置是否合理

160 rangeCheckForAdd(index);161

162 //插入元素之后,将index之后的元素都往后移一位

163 ensureCapacityInternal(size + 1); //Increments modCount!!

164 System.arraycopy(elementData, index, elementData, index + 1,165 size -index);166 elementData[index] =element;167 size++;168 }169

170 /**

171 * 用于add和addAll方法的范围检查172 * A version of rangeCheck used by add and addAll.173 */

174 private void rangeCheckForAdd(intindex) {175 if (index > size || index < 0)176 throw newIndexOutOfBoundsException(outOfBoundsMsg(index));177 }178

179 /**

180 * 删除指定位置的元素181 * Removes the element at the specified position in this list.182 * Shifts any subsequent elements to the left (subtracts one from their183 * indices).184 *185 *@paramindex the index of the element to be removed186 *@returnthe element that was removed from the list187 *@throwsIndexOutOfBoundsException {@inheritDoc}188 */

189 public E remove(intindex) {190 //范围检查

191 rangeCheck(index);192

193 modCount++;194 E oldValue = elementData(index); //要删除指定index位置的值195

196 //计算要移动的位数

197 int numMoved = size - index - 1;198 if (numMoved > 0)199 /*

200 public static native void arraycopy(Object src, int srcPos,201 Object dest, int destPos,202 int length);203 Copies an array from the specified source array, beginning at the204 specified position, to the specified position of the destination array.205 从源数组的特殊位置开始拷贝到目标数组的特定位置206

207 System.arraycopy(elementData, index+1, elementData, index,208 size - index - 1); //size=8,index=5,8-5-1=2,src=[1,2,3,4,5,7,8]209 */

210 System.arraycopy(elementData, index+1, elementData, index,211 numMoved);212 elementData[--size] = null; //clear to let GC do its work

213

214 returnoldValue;215 }216

217 /**

218 * 移除list列表中第一次出现的待删除的元素219 * Removes the first occurrence of the specified element from this list,220 * if it is present. If the list does not contain the element, it is221 * unchanged. More formally, removes the element with the lowest index222 * i such that223 * (o==null ? get(i)==null : o.equals(get(i)))224 * (if such an element exists). Returns true if this list225 * contained the specified element (or equivalently, if this list226 * changed as a result of the call).227 *228 *@paramo element to be removed from this list, if present229 *@returntrue if this list contained the specified element230 */

231 public booleanremove(Object o) {232 //依次遍历删除

233 if (o == null) {234 for (int index = 0; index < size; index++)235 if (elementData[index] == null) {236 fastRemove(index);237 return true;238 }239 } else{240 for (int index = 0; index < size; index++)241 if(o.equals(elementData[index])) {242 fastRemove(index);243 return true;244 }245 }246 return false;247 }248 //remove函数用户移除指定下标的元素,此时会把指定下标到数组末尾的元素向前移动一个单位,249 //并把最后一个元素置为null

250

251 /**

252 * 将list表中指定位置的元素替换掉253 * Replaces the element at the specified position in this list with254 * the specified element.255 *256 *@paramindex index of the element to replace257 *@paramelement element to be stored at the specified position258 *@returnthe element previously at the specified position259 *@throwsIndexOutOfBoundsException {@inheritDoc}260 */

261 public E set(intindex, E element) {262 //范围检查

263 rangeCheck(index);264

265 //指定位置的元素值

266 E oldValue =elementData(index);267 //将elementData的指定位置替换为element

268 elementData[index] =element;269 returnoldValue;270 }271

272 /**

273 * 返回在list列表中的指定元素第一次出现的索引值274 * Returns the index of the first occurrence of the specified element275 * in this list, or -1 if this list does not contain the element.276 * More formally, returns the lowest index i such that277 * (o==null ? get(i)==null : o.equals(get(i))),278 * or -1 if there is no such index.279 */

280 public intindexOf(Object o) {281 if (o == null) {282 for (int i = 0; i < size; i++)283 if (elementData[i]==null)284 returni;285 } else{286 for (int i = 0; i < size; i++)287 if(o.equals(elementData[i]))288 returni;289 }290 return -1;291 }292 //从头开始查找元素,可以查找null值

293

294 /**

295 * 返回在list列表中指定位置的元素296 * Returns the element at the specified position in this list.297 *298 *@paramindex index of the element to return299 *@returnthe element at the specified position in this list300 *@throwsIndexOutOfBoundsException {@inheritDoc}301 */

302 public E get(intindex) {303 //范围检查,只检查>=size的部分

304 rangeCheck(index);305

306 returnelementData(index);307 }308

309 E elementData(intindex) {310 return(E) elementData[index];311 }312 //注意:返回的值都做了向下转型(object->E)处理

313 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/432477.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UE4 多个static mesh合并成一个static mesh

第一种方法&#xff1a; 1. 把需要合并的所有的static mesh导入到一个level中&#xff0c;static mesh导入到level中后&#xff0c;会自动变成static mesh actor 2. 在level的world outliner面板把选中需要合并的所有的static mesh actor 3. 鼠标右键&#xff0c;在弹出的快…

在UE中自由绘制基本图元的几种方法

1. 在UMG中可以使用UWidgetBlueprintLibrary类来绘制&#xff0c;UWidgetBlueprintLibrary类提供了一系列的静态函数可以使用。但只提供了DrawLine&#xff0c;DrawLines&#xff0c;DrawBox&#xff0c;DrawText&#xff0c;DrawTextFormatted函数&#xff0c;如果要自己绘制的…

java work set没有显示出来_eclipse里新建work set,将项目分组放在不同文件夹

想必大家的Eclipse里也会有这么多得工程......每次工作使用到的项目肯定不会太多......每次从这么大数量的工程当中找到自己要使用的,必须大规模的滚动滚动条......有点不和谐了.无意当中看到了working set自己创建了一个 workingset 并将相应的 projects 添加到 working set 当…

指针的一些问题

1、c/c语言中不少地方&#xff0c;数组和指针可以相互替换使用&#xff0c;容易让人产生一种错觉&#xff0c;指针和数组是等价的。 数组要么在静态存储区域创建&#xff0c;如全局数组&#xff1b;要么在栈上创建如函数内的数组。数组的名称对应着&#xff08;而不是指向&…

java连接imserver_java后端IM消息推送服务开发——协议

最近在一家saas企业使用Mqtt开发IM消息推送服务&#xff0c;把开发中的一些问题记录下来&#xff0c;项目仍在商用中&#xff0c;完整的消息服务包括4个模块---协议protocol&#xff0c;信令Signal&#xff0c;规则Rule&#xff0c;状态Status&#xff0c;这个主题主要是协议pr…

好多邮箱的SMTP设置

http://731771490.diandian.com/post/2011-04-20/19576550转载于:https://www.cnblogs.com/mantian/p/3828837.html

新手向:如何理解OpenGL中着色器,渲染管线,光栅化等概念

首先&#xff0c;光栅化&#xff08;Rasterize/rasteriztion&#xff09;。 这个词儿Adobe官方翻译成栅格化或者像素化。没错&#xff0c;就是把矢量图形转化成像素点儿的过程。我们屏幕上显示的画面都是由像素组成&#xff0c;而三维物体都是点线面构成的。要让点线面&#xf…

tankwar的java坦克子弹撞墙_tankwar

tankwar 是使用java开发的一个单机版的小游戏 (未使用任何游戏引擎).和90经典版的坦克大战有些不同, 这里是纯坦克之间的战争, 英雄坦克并不用保护它的家.特色:1. 游戏共设6个关卡. (支持无限关卡)关卡设置中包括敌人坦克数量, 移动速度, 子弹速度, 以及子弹的杀伤力, 炸弹数量…

材质加载

UMaterial* Material LoadObject<UMaterial>(NULL, TEXT("/Game/VertexColorMaterial"), NULL, LOAD_None, NULL);

java如何添加进程_如何创建一个进程,如何进程调用进程

java一般用线程就够了&#xff0c;多进程优势在于每个进程互不干扰&#xff0c;劣势在于太耗费内存&#xff0c;任何事情都不是绝对的&#xff0c;在编写Java程序时&#xff0c;有时候需要在Java程序中执行另外一个程序。1、启动程序Java提供了两种方法用来启动其它程序&#x…

输入学生成绩,并按升序排列 Ascending.java

import java.util.Arrays;import java.util.Scanner;public class Ascending{ public static void main(String[] args){ Scanner innew Scanner(System.in); int[] arrnew int[10]; for(int i0;i<arr.length;i){ System.out.println("请输入第"(i1)"个学生的…

java判断ftp创建目录是否成功_Java判断Ftp服务器目录是否存在,若不存在创建目录 ....

解决方法1&#xff1a;package com.soft4j.log4j;import java.io.IOException;import sun.net.ftp.FtpClient;public class FtpTest{static String middle_ftpServer "10.103.2.250";static String middle_user "ora9iftp";static String middle_passwor…

第三方静态库的使用

1. 工程根目录下创建文件夹“ThirdParty”&#xff0c;然后在此文件夹下创建“includes”和“libs”文件夹 2. 拷贝静态库的头文件和库文件到上面的文件夹下 3. 修改工程的build.cs文件&#xff0c;增加下面代码1&#xff09;在类中private string ThirdPartyPath { …

UITableView:改变 TableHeaderView 的高度

参考&#xff1a;http://stackoverflow.com/a/526825 有这么一种需求&#xff0c;在列表顶端显示一些别样的数据&#xff0c;而这个别样的数据则需要通过一个别样的 View 来展现&#xff0c;它便是 UITableView 的 tableHeaderView。 倘若 tableHeaderView 里的内容很固定&…

perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令 -电脑资料

想写一些简化管理操作的脚本&#xff0c;下面是基础脚本之一&#xff0c;对于一个从来没使用过perl脚本的我来说还是有些难度的&#xff0c;直接上代码。此脚本用于发送远程MySQL命令并且接收结果&#xff0c;功能比较简单&#xff0c;后面会渐渐完善。#!/usr/bin/perl use Get…

【翻译】使用Ext JS设计响应式应用程序

原文&#xff1a;Designing Responsive Applications with Ext JS在当今这个时代&#xff0c;用户都希望Web应用程序无论在形状还是大小上&#xff0c;既能在桌面电脑&#xff0c;也能在移动设备上使用。使应用程序能适应不同的需求渐成趋势。幸运的是&#xff0c;Ext JS 5提供…

java各种的不好_译文《最常见的10种Java异常问题》

封面&#xff1a;洛小汐译者&#xff1a;潘潘知彼知己&#xff0c;方能百战不殆。前言本文总结了有关Java异常的十大常见问题。目录检查型异常(checked) vs. 非检查型异常(Unchecked)异常管理的最佳实践箴言为什么在try代码块中声明的变量不能在catch或者finally中被引用&#…

Hadoop 开源调度系统zeus(二)

紧跟之前Hadoop 开源调度系统zeus(一) 本节主要介绍一下zeus的架构&#xff1a; 先给一个zeus的架构图 无论Master还是Worker都有一套WEB UI&#xff0c;无论从哪个上面去看&#xff0c;看到的结果都是一样的&#xff0c;实际上一般仅仅看主 Master&#xff1a;调度内核&#x…