
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 ������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月18日 23时50分13秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
计算机网络之第三章笔记--数据链路层
2021-05-12
创建型模式之简单工厂模式实例及代码操作
2021-05-12
广东外语外贸大学第三届网络安全大赛Writeup
2021-05-12
跟着燕青学分布式事务控制技术方案
2021-05-12
VS2019 报错: LINK Error 无法找到 MSCOREE.lib的解决办法
2021-05-12
2021-04-23
2021-05-12
hadoop 分布式文件系统的计算和高可用
2021-05-12
【Linux】VMware Workstation 不可恢复错误: (vcpu-0)
2021-05-12
VS中 fatal error LNK1123: 转换到 COFF 期间失败 的解决方法
2021-05-12
ant design pro v5去掉右边content区域的水印
2021-05-12
web_求和(练习)
2021-05-12
JavaScript——使用iterator遍历迭代map,set集合元素
2021-05-12
IAR调试卡顿的解决办法
2021-05-13
Course Schedule II
2021-05-13
Django ORM操作
2021-05-13
京喜小程序体验评分优化实践
2021-05-13
C#中文转换成拼音
2021-05-13
C++错误笔记
2021-05-13
【无线通信模块】GPRS DTU不稳定和容易掉线原因
2021-05-13
SpringBoot使用RedisTemplate简单操作Redis的五种数据类型
2021-05-13