java arraylist 源代码_java中ArrayList的源代码是什么
展开全部
package java.util;
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* 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 E[] 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
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}
public ArrayList() {
this(10);
}
public ArrayList(Collection extends E> c) {
size = c.size();
// Allow 10% room for growth
int capacity = (int) Math.min((size*110L)/100, Integer.MAX_VALUE);
elementData = (E[]) c.toArray(new Object[capacity]);
}
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size
Object oldData[] = elementData;
elementData = (E[])new Object[size];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity
636f707962616964757a686964616f31333363396332newCapacity = minCapacity;
elementData = (E[])new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(Object elem) {
return indexOf(elem) >= 0;
}
public int indexOf(Object elem) {
if (elem == null) {
for (int i = 0; i
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
public int lastIndexOf(Object elem) {
if (elem == 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 (elem.equals(elementData[i]))
return i;
}
return -1;
}
public Object clone() {
try {
ArrayList v = (ArrayList) super.clone();
v.elementData = (E[])new Object[size];
System.arraycopy(elementData, 0, v.elementData, 0, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
public Object[] toArray() {
Object[] result = new Object[size];
System.arraycopy(elementData, 0, result, 0, size);
return result;
}
public T[] toArray(T[] a) {
if (a.length
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
// Positional Access Operations
public E get(int index) {
RangeCheck(index);
return elementData[index];
}
public E set(int index, E element) {
RangeCheck(index);
E oldValue = elementData[index];
elementData[index] = element;
return oldValue;
}
public boolean add(E o) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = o;
return true;
}
public void add(int index, E element) {
if (index > size || index
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
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);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i
elementData[i] = null;
size = 0;
}
public boolean addAll(Collection extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
public boolean addAll(int index, Collection extends E> c) {
if (index > size || index
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
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;
}
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);
while (size != newSize)
elementData[--size] = null;
}
private void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
int expectedModCount = modCount;
// Write out element count, and any hidden stuff
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = (E[])new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=0; i
a[i] = s.readObject();
}
}
java arraylist 源代码_java中ArrayList的源代码是什么相关推荐
- java list原理_Java中ArrayList实现原理
前言 这个分类中,将会写写Java中的集合.集合是Java中非常重要而且基础的内容,因为任何数据必不可少的就是该数据是如何存储的,集合的作用就是以一定的方式组织.存储数据.这里写的集合,一部分是比较常 ...
- java arraylist 重复_Java中ArrayList去除重复元素
Java中ArrayList去除重复元素 //删除ArrayList中重复元素 public static void removeDuplicate(ArrayList list) { ...
- java arraylist 源代码_Java中ArrayList源码浅析
ArrayList基本使用 public class ArrayListTest { public static void main(String[] args) { List list = new ...
- java用arraylist求和_Java中ArrayList的使用
ArrayList类是一个特殊的数组--动态数组.来自于System.Collections命名空间:通过添加和删除元素,就可以动态改变数组的长度. 优点: 1.支持自动改变大小 2.可以灵活的插入元 ...
- java list 结构_Java中常见数据结构List之ArrayList
这里主要包含ArrayList和LinkedList. 关于Java中的集合内容, 感觉都已经被写烂了, 我这里主要是做个复习, 再从扒下源代码, 尽量用最直白的语言把里面的核心内容记录下来.仅此而已 ...
- java c 性能比较_java 中ArrayList与LinkedList性能比较
java 中ArrayList与LinkedList性能比较 今天看一框架的代码,看到有些 可以使用ArrayList的地方 使用的是 LinkedList,用到的情景是在一个循环里面进行顺序的插入操 ...
- java stack 类 效率_Java中ArrayList、LinkedList、Vector、Stack的比较
一.介绍 先回顾一下List的框架图 由图中的继承关系,可以知道,ArrayList.LinkedList.Vector.Stack都是List的四个实现类. AbstractList是一个抽象类,它 ...
- java的list和数组谁高效_java 中ArrayList与LinkedList性能比较
java 中ArrayList与LinkedList性能比较 今天看一框架的代码,看到有些 可以使用ArrayList的地方 使用的是 LinkedList,用到的情景是在一个循环里面进行顺序的插入操 ...
- JAVA中的arraylist集合_java中遍历ArrayList集合的四种方式
详细内容 ArrayList遍历:取出ArrayList集合中的数据 方式一:for循环 方式二:增强for循环:foreach 方式三:Iterator:迭代器 方式四:ListIterator:双 ...
最新文章
- 数字化绩效管理解决方案,评估周期缩短80%,成本下降60%
- ASP.NET 2.0中的表达式构造器(Expression Builder)
- android studio 控件提示大写
- 如何成为一个卓越的程序员
- PyCharm的高效使用技巧
- 用imageMagick的composite合并图片
- 如果生活中没有数学,那么。。。
- python 小说cms_用python 发 帝国cms 文章
- 如何通过索引从列表中删除元素?
- 操作元素之修改元素属性
- Vue的单页应用中如何引用单独的样式文件
- 二十一天学通VC++之创建用户界面线程
- 康奈尔笔记法,高效学习方法推荐
- 计算机两个硬盘如何区分,双硬盘电脑怎么设置主从盘?
- Linux系统盘满了,如何解决。
- 哈哈哈哈哈哈不错测试一下测试一下哈哈哈哈哈哈不错测试一下测试一下
- 89c51的万年历c语言,用AT89C51与DS1302做的万年历c语言编程
- OpenCV——硬币检测与计数的设计实现
- R语言实验报告【全集】
- jQuery.Deferred() 方法
热门文章
- 字符串之数组中两个字符串的最小距离
- stringcstdlibctimecstdargctimectypecmathclimits
- apache php url重写语法,apache url重写实现伪静态
- mysql与oracle在软件测试_Oracle和MySQL的一些简单命令对比
- 电脑怎么测试硬盘的读写速度_两块硬盘合二为一,电脑读写翻倍?这样的“好事”你必须得了解...
- 博士毕业的人也会交“智商税”?现实远比我们想象的残酷……
- 大数据告诉你:学历真的能改变命运!!
- 东北大姐剪纸被误认为油画,遭人质疑二十多年,只因太过逼真,看完后:真香!不愧是天下第一剪!...
- 盘点那些世间顶级直男hhhhhh | 今日最佳
- 要学习数据科学知识,这些信息需要知道(数据)