ArrayList 原理(2)
1. 概述
关于Java集合的小抄中是这样描述的:
以数组实现。节约空间,但数组有容量限制。超出限制时会增加50%容量,用System.arraycopy()复制到新的数组,因此最好能给出数组大小的预估值。默认第一次插入元素时创建大小为10的数组。
按数组下标访问元素—get(i)/set(i,e) 的性能很高,这是数组的基本优势。
直接在数组末尾加入元素—add(e)的性能也高,但如果按下标插入、删除元素—add(i,e), remove(i), remove(e),则要用System.arraycopy()来移动部分受影响的元素,性能就变差了,这是基本劣势。
然后再来学习一下官方文档:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
ArrayList是一个相对来说比较简单的数据结构,最重要的一点就是它的自动扩容,可以认为就是我们常说的“动态数组”。
来看一段简单的代码:
1
2
3
4
5
|
ArrayList<String> list = new ArrayList<String>(); list.add( "语文: 99" ); list.add( "数学: 98" ); list.add( "英语: 100" ); list.remove( 0 ); |
在执行这四条语句时,是这么变化的:
其中,add
操作可以理解为直接将数组的内容置位,remove
操作可以理解为删除index为0的节点,并将后面元素移到0处。
2. add函数
当我们在ArrayList中增加元素的时候,会使用add
函数。他会将元素放到末尾。具体实现如下:
1
2
3
4
5
|
public boolean add(E e) { ensureCapacityInternal(size + 1 ); // Increments modCount!! elementData[size++] = e; return true ; } |
我们可以看到他的实现其实最核心的内容就是ensureCapacityInternal
。这个函数其实就是自动扩容机制的核心。我们依次来看一下他的具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private void ensureCapacityInternal( int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity( int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0 ) grow(minCapacity); } private void grow( int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; // 扩展为原来的1.5倍 int newCapacity = oldCapacity + (oldCapacity >> 1 ); // 如果扩为1.5倍还不满足需求,直接扩为需求值 if (newCapacity - minCapacity < 0 ) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0 ) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } |
也就是说,当增加数据的时候,如果ArrayList的大小已经不满足需求时,那么就将数组变为原长度的1.5倍,之后的操作就是把老的数组拷到新的数组里面。例如,默认的数组大小是10,也就是说当我们add
10个元素之后,再进行一次add时,就会发生自动扩容,数组长度由10变为了15具体情况如下所示:
3 set和get函数
Array的put和get函数就比较简单了,先做index检查,然后执行赋值或访问操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public E set( int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; } public E get( int index) { rangeCheck(index); return elementData(index); } |
4 remove函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public E remove( int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1 ; if (numMoved > 0 ) // 把后面的往前移 System.arraycopy(elementData, index+ 1 , elementData, index, numMoved); // 把最后的置null elementData[--size] = null ; // clear to let GC do its work return oldValue; } |
注释很清楚:
ArrayList 原理(2)的更多相关文章
- Java集合 ArrayList原理及使用
ArrayList是集合的一种实现,实现了接口List,List接口继承了Collection接口.Collection是所有集合类的父类.ArrayList使用非常广泛,不论是数据库表查询,exce ...
- ArrayList原理解析
简介 ArrayList就是动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了动态的增加和减少元素,实现了ICollection和IList接口,灵活的设置数组的大小等好处 有图有码 图 ...
- ArrayList 原理(1)
ArrayList是Java List类型的集合类中最常使用的,本文基于Java1.8,对于ArrayList的实现原理做一下详细讲解. (Java1.8源码:http://docs.oracle.c ...
- 「必知必会」最细致的 ArrayList 原理分析
从今天开始也正式开 JDK 原理分析的坑了,其实写源码分析的目的不再是像以前一样搞懂原理,更重要的是看看他们编码风格更进一步体会到他们的设计思想.看源码前先自己实现一个再比对也许会有不一样的收获! ...
- 一.ArrayList原理及实现学习总结
一.ArrayList介绍 ArrayList是一种线性数据结构,它的底层是用数组实现的,相当于动态数组.与Java中的数组相比,它的容量能动态增长.类似于C语言中的动态申请内存,动态增长内存. 当创 ...
- ArrayList原理(一)
需要使用到动态数组的时候用的最多的就是ArrayList了,底层其实是Object数组,以下demo基于JDK1.8: List<Integer> list = new ArrayLis ...
- ArrayList原理分析(重点在于扩容)
首先,ArrayList定义只定义类两个私有属性: /** * The array buffer into which the elements of the ArrayList are stored ...
- 容器ArrayList原理(学习)
一.概述 动态数组,容量能动态增长,元素可以为null,用数组存储,非线程同步(vector线程同步) 每个 ArrayList 实例都有一个容量,该容量是指用来存储列表元素的数组的大小,自动增长(默 ...
- Day 5 :ArrayList原理、LinkedList原理和方法和迭代器注意事项
迭代器在变量元素的时候要注意事项: 在迭代器迭代元素 的过程中,不允许使用集合对象改变集合中的元素个数,如果需要添加或者删除只能使用迭代器的方法进行操作. 如果使用过了集合对象改变集合中元素个数那 ...
随机推荐
- task optimization之superglue分析
开启logging (例子F:\wamp\www\git_repos\GitHub\GeneralUtility\superglue-master\examples\src\logging.cpp) ...
- python下爬某个网页的图片
#coding=utf-8 import re import urllib def getHtml(url): #获取url对应得源码 page = urllib.urlopen(url) html ...
- qt creator 快捷键 (一)
F1 查看帮助F2 跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2 声明和定义之间切换F4 头文件和源文件之间切换Ctrl+1 ...
- TextBox(只允许输入字母或者数字)——重写控件
实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...
- 【Hibernate学习笔记-5】@Formula注解的使用
ORM映射关系:注解方式 package org.crazyit.app.domain; import javax.persistence.*; import org.hibernate.annota ...
- 常用的sql语句(存储过程语法)
1.存储过程语法 ①package create or replace package PKG_RPT_WAREHOUSE is -- Author : -- Created : 2018/9/28 ...
- [转]C#调用Excel VBA宏
[转载自]http://www.shangxueba.com/jingyan/95031.html 附上一段原创常用代码 计算列标题字符串 Function CalcColumn(ByVal c As ...
- 1050 String Subtraction (20 分)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be the ...
- [UE4]头文件循环依赖C++
有2个类:aaa和bbb. aaa.h已经#include了bbb.h,则bbb.h就不能#include aaa.h,但bbb.cpp可以#include aaa.h bbb.h已经#include ...
- Javascript框架
网易开源框架http://www.oschina.net/p/nej http://www.linuxeden.com/html/develop/20120716/127404.html 16 款最流 ...