今天开始打算将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. 两个有效的插入删除的方法。

Methods 
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里一个重要方法的源码了。

想想就有点小激动~~~~

一天一个类--ArrayList之一的更多相关文章

  1. 一天一个类--ArrayList之二

    继续我的小激动--- 1.看看构造一个ArrayList 有两种方式 一个指定大小,一个不指定.我们知道他其实使用数组来实现了,数组肯定要有大小,那么他没指定大小,默认的是多少呢???追踪源码---开 ...

  2. java 集合之实现类ArrayList 和 LinkedList

    List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...

  3. java的List接口的实现类 ArrayList,LinkedList,Vector 的区别

    Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. Array ...

  4. java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析

    java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...

  5. 类ArrayList

    什么是ArrayList类 Java提供了一个容器 java.util.ArrayList 集合类,他是大小可变的数组的实现,存储在内的数据称为元素.此类提供一些方法来操作内部存储的元素. Array ...

  6. 集合(1)—List接口的实现类ArrayList

    List List接口是Collection接口的子接口,从其名称可以看出,是一个元素有序(并不是按大小排序,具有顺序索引,类似于数组),默认按照元素的添加顺序设置元素的索引. List用法 List ...

  7. Java总结 - List实现类ArrayList&LinkedList

    本文是根据源码进行学习的,如果我有什么理解不对的地方请多指正,谢谢您 上面基本就是List集合类的类图关系了,图中省略掉了比如Cloneable等标记接口,那么List分别具体的主要实现类有:Arra ...

  8. 一天一个类--NIO 之Buffer

    java.nio  --- 定义了 Buffer 及其数据类型相关的子类.其中被 java.nio.channels 中的类用来进行 IO 操作的 ByteBuffer 的作用非常重要. java.n ...

  9. 一天一个类 --- StringTokenizer

    这是一个将字符串按照指定的delimiters(分隔符)进行分割的类. 首先看看他的构造函数: public StringTokenizer(String str, String delim, boo ...

随机推荐

  1. Leetcode 动态规划 Unique Paths

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...

  2. [置顶] 如何在Windows 7 64位安装Python,并使用Matplotlib绘图

    1.     安装Python 我使用的是Windows 7 64 bit,所以我从Python官网下载python-2.7.5.amd64.msi,安装步骤如下: 1)        安装windo ...

  3. HDU 1465 不容易系列之排错

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 大家常常感 ...

  4. 【MFC相关】图片显示

    CPaintDC dc(this); if (!m_hBitmap) return; image.Attach(m_hBitmap); CRect rect; GetClientRect(&r ...

  5. Java map取value最大值和最小值

    /** * 求Map<K,V>中Value(值)的最小值 * * @param map * @return */ public static Object getMinValue(Map& ...

  6. Android中利用httpclient进行网络通信的方法(以用户登录为例说明)

    http://www.android100.org/html/201406/09/22915.html 1.服务器端 服务器端和android没有太大关系,对J2EE比较熟悉的话写起来应该很容易,这里 ...

  7. python成长之路10

    断点续传   python2.7 多继承  py35多继承   socketserver源码     支持并发处理socket   i/o多路复用   上节回顾     socket          ...

  8. asp.net core+ef core

    asp.net core+ef core 官方的文档https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html 先来看一 ...

  9. 条码知识之九:EAN-128条码(上)

     EAN-128码,现称GS1-128码,是专用于GS1系统中的条码,可以标注商品的附加信息,在商品信息的标识.产品的跟踪与追溯中有广泛的用途. EAN-128码来自于CODE-128码,在字符集.条 ...

  10. 17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据

    17.1.1.8?Setting Up Replication with Existing Data设置复制使用存在的数据 当设置复制使用存在的数据,你需要确定如何最好的从master 得到数据到sl ...