今天开始打算将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. Objective-c Category(类别)

    category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...

  2. C# inherit

    Case:class A has a construct. class B is inherit from class A and B also has a construct. What's the ...

  3. 融合python2和python3

    很多情况下你可能会想要开发一个程序能同时在python2和python3中运行. 想象一下你开发了一个模块,成百上千的人都在使用它,但不是所有的用户都同时使用python 2和3.这种情况下你有两个选 ...

  4. [C#参考]事件和委托的关系

    前面说了委托,接下来就要说一说事件了,同时最后再说一下委托和事件的区别. 事件和委托很相似,事件就好像是被简化的针对特殊用途的委托.看下面的图: 从这张图中能看到,事件是发布者的一个成员,它不是类型. ...

  5. Spring AOP基于xml配置实例

    SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象.为了更好地理解记忆,这里几下我自己的通俗的理解. 切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法 ...

  6. Android操作HTTP实现和服务器通信

    众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式.至于Socket通信会在以后的博文中介绍. HTTP协议简介: HT ...

  7. XWalkView+html 开发Android应用

    在Android开发中有时候为了开发简洁和方便移植,采用了Html+WebView的开发模式,然而Android自带的WebView控件是调用的本机的浏览器内核,有些版本较老的手机浏览器和手机性能都不 ...

  8. Qt Creator插件工作流程代码走读

    Qt Creator有个很风骚的插件管理器PluginManager,还有个很骚包的插件说明PluginSpec.基本上,所有的Qt程序的入口都是传统的C程序一样,代码流程从main()函数开始.  ...

  9. Java 如何判断一个字符是否是数字或字母

    在C++中, 可以用isdigit()判断一个字符是否是数字,可以用isalpha()判断一个字符是否是字母,还有很多,都在<cctype>头文件中 而类似的方法在JAVA中,则主要是Ch ...

  10. Ubuntu 13.04 安装使用clang

    其实很简单,就是用命令即可: apt-get install clang-3.2 clang-3.2-doc 主要说明一点,/usr/bin/c++链接原来指向g++,现在被改变了. $ ls /us ...