clone一个副本:
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
转换为数组:
public Object[] toArray() {
return Arrays.copyOf(elementData, size);//调用Arrays.copyOf()方法
}
下面是转换为泛型数组:
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
范围检查:臭名昭著的 IndexOutOfBoundsException异常
private void RangeCheck(int index) {
if (index >= size)//数组越界,这里没有判断小于0的情况
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
通过下标得到一个元素:
public E get(int index) {
RangeCheck(index);//先检查是否越界
return (E) elementData[index];//返回的是数组中的下标 }
通过下标和一个元素赋值,返回的是原先的值:
public E set(int index, E element) {
RangeCheck(index);//先检查是否越界
E oldValue = (E) elementData[index];//通过临时变量把当前下标的值保存
elementData[index] = element;//赋值
return oldValue;//注意返回的是当前下标的原先值
}
添加一个新的元素到末尾,前面说道新增方法都要先调用ensureCapacity方法:
public boolean add(E e) {
ensureCapacity(size + 1); //大小加一 // Increments modCount!!
elementData[size++] = e;//size默认是0所以是从0开始赋值
return true;
}
API文档中的说明是:将指定的元素插入此列表中的指定位置。向右移动当前位于该位置的元素(如果有)以及所有后续元素(将其索引加 1)。通俗的说法是在指定位置插入元素,指定元素和后面的元素后移
这个方法和set(int index, E element) 不一样,set只是把元素赋值给指定的下标同时返回下标的原先值.
add(int index, E element)的判断越界是通过元素的大小来判断的
所以如果
ArrayList list=new ArrayList();
list.add(1, 8);
//报错,因为size元素大小还是0
//如果l
list.add(0,"")//就可以
如果一致add同一下标所有后续元素索引加1
如下:
ArrayList list=new ArrayList();
list.add(0, 8);
list.add(0, 8);
list.add(0, 8);
System.out.println(list);
//结果为[8, 8, 8]
:
public void add(int index, E element) {
if (index > size || index < 0)//判断是否越界,注意这里是以元素的个数来判断的
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
//源数组中位置在 srcPos 到 srcPos+length-1 之间的组件被分别复制到
//目标数组中的 destPos 到 destPos+length-1 位置
elementData[index] = element;
size++;//元素加一
}
删除指定位置的元素,返回被删除的元素,由于ArrayList采用一个对象数组存储元素,所以在删除一个元素时需要把后面的元素前移。删除一个元素时只是把该元素在elementData数组中的引用置为null,具体的对象的销毁由垃圾收集器负责
public E remove(int index) {
RangeCheck(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; // Let gc do its work
return oldValue;//返回删除前的数据
}
内部删除方法,跳过越界检查,不返回删除元素的值:ArrayList内部调用的删除方法
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
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 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;
}
清空列表:
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;//设定元素大小为0
}
添加集合c中的元素到ArrayList的末尾,添加成功返回true,如果集合c为空,返回false。
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 < 0)//判断越界
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);//这里是等于0的情况也就是说直接在数组后面加
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;
}