标签:
1,继承结构图:/**
* The root interface in the <i>collection hierarchy</i>. A collection
* represents a group of objects, known as its <i>elements</i>. Some
* collections allow duplicate elements and others do not. Some are ordered
* and others unordered.
package java.lang;
import java.util.Iterator;
/** Implementing this interface allows an object to be the target of
* the "foreach" statement.
* @since 1.5
*/
public interface Iterable<T> {
/**
* Returns an iterator over a set of elements of type T.
*
* @return an Iterator.
*/
Iterator<T> iterator();
}
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @author Josh Bloch
* @author Neal Gafter
* @version 1.55, 04/21/06
* @see Set
* @see List
* @see Map
* @see SortedSet
* @see SortedMap
* @see HashSet
* @see TreeSet
* @see ArrayList
* @see LinkedList
* @see Vector
* @see Collections
* @see Arrays
* @see AbstractCollection
* @since 1.2
*/
public abstract class AbstractCollection<E> implements Collection<E>
/**
* {@inheritDoc}
*
* <p>This implementation returns <tt>size() == 0</tt>.
*/
public boolean isEmpty() {
return size() == 0;
}
public boolean contains(Object o) {
Iterator<E> e = iterator();
if (o==null) {
while (e.hasNext())
if (e.next()==null)
return true;
} else {
while (e.hasNext())
if (o.equals(e.next()))
return true;
}
return false;
}
public boolean remove(Object o) {
Iterator<E> e = iterator();
if (o==null) {
while (e.hasNext()) {
if (e.next()==null) {
e.remove();
return true;
}
}
} else {
while (e.hasNext()) {
if (o.equals(e.next())) {
e.remove();
return true;
}
}
}
return false;
}
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
Iterator<? extends E> e = c.iterator();
while (e.hasNext()) {
if (add(e.next()))
modified = true;
}
return modified;
}
An ordered collection (also known as a <i>sequence</i>).
Unlike sets, lists typically allow duplicate elements.
The <tt>List</tt> interface provides a special iterator, called a
* <tt>ListIterator</tt>, that allows element insertion and replacement, and
* bidirectional access in addition to the normal operations that the
* <tt>Iterator</tt> interface provides.
List list = Collections.synchronizedList(new ArrayList(...));
public class TestAdd {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
List<Integer> lists = new ArrayList<Integer>();
//测试add方法
for(int i = 0 ; i < 1000000; i++){
lists.add(1);
}
long endTime = System.currentTimeMillis();
System.out.println("add添加1000000元素,用时: " + (endTime - startTime));//输出32
//add 一个元素的时间
startTime = System.currentTimeMillis();
lists.add(2);
endTime = System.currentTimeMillis();
System.out.println("add添加1元素,用时: " + (endTime - startTime));//输出0
//get 一个元素
startTime = System.currentTimeMillis();
lists.get(2);
endTime = System.currentTimeMillis();
System.out.println("get元素,用时: " + (endTime - startTime));//输出0
}
}
long startTime = System.currentTimeMillis();
List<Integer> lists = new ArrayList<Integer>(1000000);
//测试add方法
for(int i = 0 ; i < 1000000; i++){
lists.add(1);
}
long endTime = System.currentTimeMillis();
System.out.println("add添加1000000元素,用时: " + (endTime - startTime));//输出12
startTime = System.currentTimeMillis();
Iterator<Integer> iter = lists.iterator();
int a ;
while(iter.hasNext()){
a = iter.next();
}
endTime = System.currentTimeMillis();
System.out.println("迭代器元素,用时: " + (endTime - startTime));//输出33
private transient Object[] elementData;
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList() {
this(10);
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
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);
}
}
public E remove(int index) {
RangeCheck(index);//确保索引位置未超出最大size,超出则抛出异常。
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;
}
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
标签:
原文地址:http://blog.csdn.net/mergades/article/details/42917301