Java 中 ArrayList 常用函数及其实现方法
发布时间:2023-06-21 13:22:40
ArrayList是Java中常用的集合类之一,它是Java中提供的动态数组,能够根据需要动态增加或减少容量。由于它可以存储任意类型的对象,因此在Java的程序中使用非常广泛。在本篇文章中,我们将主要讨论ArrayList的常用函数及其实现方法。
1. add
添加元素到ArrayList的末尾。
boolean add(E element)
实现方法:
public boolean add(E element) {
ensureCapacity(size + 1);
elementData[size++] = element;
return true;
}
2. addAll
添加一个集合的所有元素到ArrayList的末尾。
boolean addAll(Collection<? extends E> c)
实现方法:
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew);
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
3. clear
清空ArrayList中的所有元素。
void clear()
实现方法:
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
4. clone
返回一个ArrayList的浅拷贝(副本)。
Object clone()
实现方法:
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);
}
}
5. contains
判断ArrayList是否包含某个元素。
boolean contains(Object o)
实现方法:
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
private 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;
}
6. get
获取ArrayList中指定位置的元素。
E get(int index)
实现方法:
public E get(int index) {
rangeCheck(index);
return (E) elementData[index];
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
7. indexOf
获取指定元素在ArrayList中的 次出现的位置。
int indexOf(Object o)
实现方法:
private 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;
}
8. isEmpty
判断ArrayList是否为空。
boolean isEmpty()
实现方法:
public boolean isEmpty() {
return size == 0;
}
9. iterator
返回一个用于遍历ArrayList的迭代器。
Iterator<E> iterator()
实现方法:
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int cursor;
int lastRet = -1;
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
10. remove
移除ArrayList中某个位置的元素。
E remove(int index)
实现方法:
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; // clear to let GC do its work
return oldValue;
}
11. size
获取ArrayList中元素的个数。
int size()
实现方法:
public int size() {
return size;
}
12. toArray
将ArrayList转为一个对象数组。
Object[] toArray()
实现方法:
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
以上就是ArrayList中常用函数的实现方法。ArrayList在Java中使用非常频繁,因此我们需要掌握它的使用方法,熟练掌握这些函数的实现方法将有助于我们更好地使用ArrayList。
