说明:An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
继承关系:Collection->list->ArrayList
主要的函数: boolean
add(E e)
Appends the specified element to the end of this list
void
add(int index, E element)
Inserts the specified element at the specified position in this list
Object get(int index)
Returns the element at the specified position in this list.
int
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
boolean
isEmpty()
Returns true if this list contains no elements.
Object
remove(int index)
Removes the element at the specified position in this list
boolean
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present
int
size()
Returns the number of elements in this list.
Object[]
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).ArrayList~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ArrayList()
Constructs an empty list with an initial capacity of ten.
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
void
clear()
Removes all of the elements from this list.
E
get(int index)
Returns the element at the specified position in this list.
Eclipse常用快捷键:
alt + / 快速填充函数 ctrl + shift + o 自动import
windows->preference->java->formatter 设置格式化
windows->preference->Editor->Content assist 去掉fill mthod arguments and show guessed
主要源代码:
public ArrayList() {
this(10);
}
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
private transient Object[] elementData;
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}