ArrayList源码分析
发布日期:2021-05-14 09:35:24 浏览次数:21 分类:精选文章

本文共 7023 字,大约阅读时间需要 23 分钟。

ArrayList ������������������

ArrayList������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


������������

1. ������������������

public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

���������������������������������������������������������������������������������������������������������������������������������10������������������

2. ������������������������

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);
}
}
  • ��� initialCapacity ������0���������������������������������
  • ��� initialCapacity ���0���������������������������
  • ������������������������������������������

3. ������������������

public ArrayList(Collection
c) {
elementData = c.toArray();
if (elementData.length != 0) {
if (elementData.getClass() != Object[].class) {
elementData = Arrays.copyOf(elementData, elementData.length, Object[].class);
}
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
  • ���������������������������������������������������Object���������������������������
  • ������������������������������������������

������������

1. ������������

public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
  • ���������������������������������������������������������������������������
  • ��������������� ensureCapacityInternal ������������������������������������������������������������1.5������

2. ������������������

public void add(int index, E element) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
ensureCapacityInternal(size + 1);
// ���������������������������
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}
  • ������������������������
  • ���������������������������������
  • ������������������������������������

3. ������������������

public boolean addAll(Collection
c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
  • ������������������������������������������������������������������������������

4. ������������������������

public boolean addAll(int index, Collection
c) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
int numMoved = size - index;
if (numMoved > 0) {
System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
}
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
  • ������������������������
  • ���������������������������������������������������������

������������

1. ������������

public E remove(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
modCount++;
int oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return oldValue;
}
  • ���������������������������
  • ���������������������������������������������������������

2. ������������

public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++) {
if (elementData[index] == null) {
fastRemove(index);
return true;
}
}
} else {
for (int index = 0; index < size; index++) {
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
}
return false;
}
  • ������null������������������������ fastRemove ���������������������

3. ������������

protected void removeRange(int fromIndex, int toIndex) {
if (toIndex < fromIndex) {
throw new IndexOutOfBoundsException("toIndex < fromIndex");
}
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved);
size -= (toIndex - fromIndex);
for (int i = size; i < size - (toIndex - fromIndex); i++) {
elementData[i] = null;
}
}
  • ���������������������������������������������������������������������������

4. ������

public void clear() {
modCount++;
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
  • ���������������������������������������������0���

������������

1. ������������������

public E set(int index, E element) {
if (index >= size) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
  • ���������������������������������������

2. ������������

public E get(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
return (E) elementData[index];
}
  • ���������������������������������������������������

3. element������������

public boolean contains(Object o) {
return indexOf(o) >= 0;
}
  • ������ indexOf ������������������������������������������

4. ������������������

public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++) {
if (elementData[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < size; i++) {
if (o.equals(elementData[i])) {
return i;
}
}
}
return -1;
}
  • ���������������������null������������������������������������������������

5. ������������������

public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size - 1; i >= 0; i--) {
if (elementData[i] == null) {
return i;
}
}
} else {
for (int i = size - 1; i >= 0; i--) {
if (o.equals(elementData[i])) {
return i;
}
}
}
return -1;
}
  • ������������������������������null������

������������������

1. ������

public int size() {
return size;
}
  • ���������������������������������������������������������

2. ���������

public boolean isEmpty() {
return size == 0;
}
  • ������������������������������������

��������������������������������������� ArrayList ������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:LinkedList源码分析
下一篇:IntentService源码分析

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月18日 23时50分13秒