一、ArrayList
众所周知数组是一个有效的数据存储集合,但是对于数组的构建需要实现对数组元素的个数进行相应的估计,因此需要一个能够动态创建储存数据的动态数组,ArrayList就是一个可以动态创建数组的一个方法容器。前面的超详细的逐句讲解Java高级接口之ArrayList底层源码讲解(一)主要介绍ArrayList的创建方法,以及其初始化的相关设置,这一步,我将介绍ArrayList所具有的方法。
二、ArrayList方法
public int size() {
return size;
}
返回此列表中的元素数,返回值为int类型。
public boolean isEmpty() {
return size == 0;
}
判断ArrayList是否为空,返回值为Boolean类型。
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
判断ArrayList是否为空,返回值为Boolean类型。
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;
}
用于判断该元素是否被ArrayList包含,如果被包含则返回下标,如果未被找到则返回-1,返回值类型为int类型。
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;
}
这个方法与indexof方法一样都是用于元素是否被包含,该方法 是从后向前查找,如果查到就返回下标,没有查到就返回-1,返回值为整型。
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) 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(e);
}
}
该方法主要用于克隆一个ArrayList数组,该复制方式是借助Arrays的copyOf方法实现的。
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
这个是tostring方法的重写,主要将ArrayList数组的元素转化为字符串。
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;
}
以适当的顺序(从第一个元素到最后一个元素)返回一个包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的类型。如果列表适合指定的数组,则在其中返回。否则,将使用指定数组的运行时类型和此列表的大小分配一个新数组。
E elementData(int index) {
return (E) elementData[index];
}
定义ArrayList数组的数据元素存储结构
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
获取ArrayList数组中下标为index的元素
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
主要设置ArrayList数组中下标为index的位置的元素。
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
在ArrayList数组后面追加元素
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
将某个元素插入到ArrayList数组某个位置,如果该位置有元素,其他元素向后移动。
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; // clear to let GC do its work
return oldValue;
}
移除ArrayList数组的下标为index的元素。
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;
}
删除元素,如果返回true就删除成功,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; // clear to let GC do its work
}
快速删除,主要用于ArrayList数组元素的删除。
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
清空ArrayList数组元素。
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(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) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(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;
}
从指定位置开始,将指定集合中的所有元素插入到此列表中。将当前在该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。新元素将按照指定集合的迭代器返回的顺序出现在列表中。