ArrayList 源码分析
// 默认情况下,数组的初始化大小
private static final int DEFAULT_CAPACITY = 10;
// 空数组
private static final Object[] EMPTY_ELEMENTDATA = {};
// 空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 数据
transient Object[] elementData; // non-private to simplify nested class access
// 数据大小
private int size;
// 给数组指定初始化大小
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
// 不指定大小的话默认给数组指定初始化大小为10
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
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;
// 默认情况下扩充为原来的一半
int newCapacity = oldCapacity + (oldCapacity >> 1);
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);
}
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public E remove(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(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; // clear to let GC do its work
return oldValue;
}
public int size() {
return size;
}
// 通过 native 层去拷贝代码
// src :原来的数组
// srcPos:原来数组的开始位置
// dest:新的数组
// destPos:新数组的开始位置
// length:拷贝多少个
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
通过上面的代码来分析,ArrayList 其内部的实现方式其实就是数组,如果没指定数组的大小,那么在第一次添加数据的时候,数组的初始大小是 10 ,每次当不够用的时候默认会扩充原来数组的 1/2 ,每次扩充数组大小都会涉及到创建新数组和数据的拷贝复制。而数组的拷贝和逻动都是由我们的 native 层代码实现,接下来我们简单实现native层ArrayList逻辑。
实现 Native 层的 ArrayList
#include<malloc.h>
#include<memory.h>
#include <iostream>
using namespace std;
template<class E>
class ArrayList
{
public:
//数组头指针
E* elementData = NULL;
//数组长度
int index = 0;
//数组容量大小
int size = 0;
public:
ArrayList();
ArrayList(int size);
ArrayList(const ArrayList& list);
~ArrayList();
public:
bool add(E e);
int len();
int capacity();
E get(int index);
E remove(int index);
private:
void ensureCapacityInternal(int minCapacity);
void grow(int minCapacity);
};
template<class E>
ArrayList<E>::ArrayList()
{
cout << "无参构造" << endl;
}
template<class E>
ArrayList<E>::ArrayList(int size)
{
cout << "带参构造" << endl;
if (size == 0) {
return;
}
this->size = size;
this->elementData = (E*)malloc(sizeof(E) * size);
}
template<class E>
ArrayList<E>::ArrayList(const ArrayList& list)
{
cout << "拷贝构造" << endl;
this->size = list.size;
this->index = list.index;
this->elementData = (E*)malloc(sizeof(E) * size);
memcpy(this->elementData, list.elementData, sizeof(E) * size);
}
template<class E>
ArrayList<E>::~ArrayList()
{
cout << "析构函数" << endl;
if (this->elementData) {
free(this->elementData);
this->elementData = NULL;
}
}
template<class E>
bool ArrayList<E>::add(E e)
{
ensureCapacityInternal(index + 1);
this->elementData[index++] = e;
return true;
}
template<class E>
int ArrayList<E>::len()
{
return this->index;
}
template<class E>
int ArrayList<E>::capacity()
{
return this->size;
}
template<class E>
E ArrayList<E>::get(int index)
{
return this->elementData[index];
}
template<class E>
E ArrayList<E>::remove(int index)
{
E oldValue = this->elementData[index];
int needMoved = this->index - index - 1;
int i = 0;
for (i; i < needMoved; i++)
{
this->elementData[index + i] = this->elementData[index + i + 1];
}
this->index -= 1;
return oldValue;
}
template<class E>
void ArrayList<E>::ensureCapacityInternal(int minCapacity)
{
if (this->elementData == NULL)
{
minCapacity = 10;
}
if (minCapacity - size > 0)
{
grow(minCapacity);
}
}
template<class E>
void ArrayList<E>::grow(int minCapacity)
{
int newCapacity = size + (size >> 1);
if (newCapacity - minCapacity < 0)
{
newCapacity = minCapacity;
}
E* new_arr = (E*)malloc(sizeof(E) * newCapacity);
if (this->elementData)
{
memcpy(new_arr, this->elementData, sizeof(E) * index);
free(this->elementData);
}
this->elementData = new_arr;
size = newCapacity;
}
int main() {
//ArrayList<int> list = { 4 };
//ArrayList<int> list(4);
//list.add(1);
//list.add(2);
//list.add(3);
//int i = 0;
//for (i; i < list.index; i++)
//{
// cout << "i:" << list.get(i) << endl;
//}
ArrayList<int>* list = new ArrayList<int>(5);
list->add(1);
list->add(2);
list->add(3);
cout << "remove 前 len:" << list->len() << " capacaity:" << list->capacity() << endl;
int i = 0;
for (i; i < list->index; i++)
{
cout << "i:" << list->get(i) << endl;
}
list->remove(1);
cout << "remove 后 len:" << list->len() << " capacaity:" << list->capacity() << endl;
i = 0;
for (i; i < list->index; i++)
{
cout << "i:" << list->get(i) << endl;
}
getchar();
return 0;
}
运行结果
带参构造
remove 前 len:3 capacaity:5
i:1
i:2
i:3
remove 后 len:2 capacaity:5
i:1
i:3
网友评论