今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的。所以笔者决定首先将从一些容器下手。【好欺负^_^】
位于顶层的是Collection,这个是一个接口,就是“祖宗”啊~
具体介绍可以参见API
常见的方法:
boolean |
add(E e)
Ensures that this collection contains the specified element (optional operation). |
boolean |
addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation). 使用泛型,一次性加入所有元素 |
void |
clear()
Removes all of the elements from this collection (optional operation). |
boolean |
contains(Object o)
Returns true if this collection contains the specified element. |
boolean |
containsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection. |
boolean |
equals(Object o)
Compares the specified object with this collection for equality. |
int |
hashCode()
Returns the hash code value for this collection. |
boolean |
isEmpty()
Returns true if this collection contains no elements. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this collection. |
boolean |
remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). |
boolean |
removeAll(Collection<?> c)
Removes all of this collection‘s elements that are also contained in the specified collection (optional operation). |
boolean |
retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
int |
size()
Returns the number of elements in this collection. |
Object[] |
toArray()
Returns an array containing all of the elements in this collection. |
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
由于Collection是个接口,其中的方法尚未实现,所以没什么可以赘述的。
接下来看看他的子接口没有什么特别的地方,
http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4
public interface List<E> extends Collection<E>
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.
这是一个有序的集合(或者序列),在插入元素的时候,受到严格的控制,同时呢,可以使用下标来访问元素。
The List interface provides four methods for positional (indexed) access to list elements。提供四个通过下表(从0开始)访问元素的方法。
同时,还提供了迭代器,来实现对元素的操作。
The List interface provides two methods to search for a specified object。还提供2方法实现对象的查找。
The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list. 两个有效的插入删除的方法。
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list (optional operation). |
void |
add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation). |
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection‘s iterator (optional operation). |
boolean |
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation). |
void |
clear()
Removes all of the elements from this list (optional operation). |
boolean |
contains(Object o)
Returns true if this list contains the specified element. |
boolean |
containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the specified collection. |
boolean |
equals(Object o)
Compares the specified object with this list for equality. |
E |
get(int index)
Returns the element at the specified position in this list. |
int |
hashCode()
Returns the hash code value for 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. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence. |
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. 返回最后一个存在的元素下标,不存在的话,返回-1 |
ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper sequence). |
ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
E |
remove(int index)
Removes the element at the specified position in this list (optional operation). |
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation). |
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection (optional operation). |
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection (optional operation). |
E |
set(int index, E element)
Replaces the element at the specified position in this list with the specified element (optional operation).、 实现按照元素位置的替换。 |
int |
size()
Returns the number of elements in this list. |
List<E> |
subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. 按照起始位置,返回子列表 |
Object[] |
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. |
上面这个颜色的表示List他爸爸没有的方法。
技巧:我们没有找到如何实现指定区间来删除元素,咋个办呢?感谢API 给我们提供了一个好的方式。
list.subList(from,to).clear(); bingo.完成任务~~~
【ArrayList】 第一个实现类
他下实现的接口不少
【惊天大秘密】 他可以加入null. 当然加入null之后,元素个数要+1的。
还有,这个实现不是线程安全的。但是API还说了一种实现了线程安全的方式:List list = Collections.synchronizedList(new ArrayList(...)); 【其实,不止这一个同步,还有set,map】
接下来,我们就要真正看看ArrayList里一个重要方法的源码了。
想想就有点小激动~~~~