ArrayList这个容器实质上就是个会自动增长的数组,默认初始容量是10,按原容器的1.5倍扩容。在ArrayList里面定义了一个私有的数组。
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this(10);
}
可以看到无参的构造方法默认调用的是有容量的构造方法,并且初始化给了10。也就是一个数组长度为10的Object类型的数组,从这里也可以看出ArrayList只能存放对象而不能存放原生数据。接下来是最重要的往容器添加数据的方法。
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
在往容器添加数据的方法中可以看到每添加一个元素都会调用一次ensureCapacity()方法,也就是添加容器长度的方法。但是传的参数是size+1,而size是容器包含的元素个数。所以每次添加的是包含元素个数+1的值。当然这里会有个判断,但元素个数+1超过原有数组长度的时候,就会执行扩容的动作。新数组的容量会是原数组的1.5倍。int newCapacity = (oldCapacity * 3)/2 + 1,接着会执行一个数组复制的动作。所以说到底ArrayList就是一个会自动增长长度的数组。从add()方法中也可以看出在ArrayList中可以增加重复的元素,并不会判断元素是否相等。
分享到:
相关推荐
c#重写ArrayList源代码。 开发语言:C#. 开发思想:oop
希望大家多多支持,此文档为转载的,但是很不错的,忘了出处了,希望原著作者原谅。
使用对象ArrayList填充DataGrid,C#源代码ArrayList MyList = new ArrayList();
C#160使用对象ArrayList填充DataGrid 源代码
《Vector、ArrayList、List使用深入剖析》-JAVA中文站(www_java-cn_com).htm
java下的一个demo,关于arraylist的用法
自己写的ArrayList,能实现添加、同级个数、获取值
ArrayList深度剖析与简单实用、 ArrayList重要的方法和属性(1)构造器 ArrayList提供了三个构造器 Count属性和Capacity属性 4、ArrayList与数组转换 Array的复杂版本 ArrayList常用方法 ArrayList同步机制 ...
Java学生成绩管理系统源代码: imporjava.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io....
集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试
vb源代码 关于arraylist相关用法
arrayList排序arrayList排序arrayList排序
在jni中操作arraylist对象,然后添加一个int型数据进去
一个C++(Ubuntu16.04+QT5.9.1)通过JNI,调用JAVA类及方法的示例。通过JNI传递和返回多种类型的参数,boolean ,int,String,ArrayList,ArrayList嵌套ArrayList<ArrayList<String>>等。
JButton picView = new JButton("查看源代码"); JButton picExit = new JButton("退出"); JLabel label=new JLabel ("地址"); JButton button=new JButton ("转向"); Box adress=Box.createHorizontalBox ...
import java.util.ArrayList; import java.util.List; import com.wsy.model.Back; import com.wsy.model.BookInfo; import com.wsy.model.BookType; import com.wsy.model.Borrow; import com.wsy.model.Operater;...
ArrayList<Shop> list = (ArrayList)session.getAttribute(ISysConstans.CARNAME); if(list!=null){ for(int i=0;i();i++){ Shop s = list.get(i); float sum = s.getCount()*s.getSPrice...
import java.util.ArrayList;//最大限度地减少实现“随机访问”数据存储(如数组)支持的该接口所需的工作。 import java.awt.image.ConvolveOp; import java.awt.Point;//坐标 import java.awt.geom.*; public ...
arraylist .
俄罗斯方块源代码+素材+音乐 一应具全 ublic class BaseShape extends Block { int col = 5; int row; public ArrayList<Block> blocks; public static final int ROTATE = 0; public static final int LEFT = ...