一天一个类--ArrayList之一
今天开始打算将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里一个重要方法的源码了。
想想就有点小激动~~~~
一天一个类--ArrayList之一的更多相关文章
- 一天一个类--ArrayList之二
继续我的小激动--- 1.看看构造一个ArrayList 有两种方式 一个指定大小,一个不指定.我们知道他其实使用数组来实现了,数组肯定要有大小,那么他没指定大小,默认的是多少呢???追踪源码---开 ...
- java 集合之实现类ArrayList 和 LinkedList
List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...
- java的List接口的实现类 ArrayList,LinkedList,Vector 的区别
Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. Array ...
- java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析
java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...
- 类ArrayList
什么是ArrayList类 Java提供了一个容器 java.util.ArrayList 集合类,他是大小可变的数组的实现,存储在内的数据称为元素.此类提供一些方法来操作内部存储的元素. Array ...
- 集合(1)—List接口的实现类ArrayList
List List接口是Collection接口的子接口,从其名称可以看出,是一个元素有序(并不是按大小排序,具有顺序索引,类似于数组),默认按照元素的添加顺序设置元素的索引. List用法 List ...
- Java总结 - List实现类ArrayList&LinkedList
本文是根据源码进行学习的,如果我有什么理解不对的地方请多指正,谢谢您 上面基本就是List集合类的类图关系了,图中省略掉了比如Cloneable等标记接口,那么List分别具体的主要实现类有:Arra ...
- 一天一个类--NIO 之Buffer
java.nio --- 定义了 Buffer 及其数据类型相关的子类.其中被 java.nio.channels 中的类用来进行 IO 操作的 ByteBuffer 的作用非常重要. java.n ...
- 一天一个类 --- StringTokenizer
这是一个将字符串按照指定的delimiters(分隔符)进行分割的类. 首先看看他的构造函数: public StringTokenizer(String str, String delim, boo ...
随机推荐
- 如何判断一个变量是数组Array类型
在很多时候,我们都需要对一个变量进行数组类型的判断.JavaScript中如何判断一个变量是数组Array类型呢?我最近研究了一下,并分享给大家,希望能对大家有所帮助. JavaScript中检测对象 ...
- Ext JS学习第十二天 Ext基础之操作dom ; get与fly 方法
此文用来记录学习笔记 •嗯!首先,什么是DOM(Document Object Model) –W3C对DOM的定义:文档对象模型是一个平台,一个中立于语言的应用程序编程接口(API),允许程序访问并 ...
- 面向对象之静态方法(static)和实例化方法的区别
这是一个经常被时时提出来的问题,很多时候我们以为理解了.懂了,但深究一下,我们却发现并不懂. 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法,问原因也说不出来所以然,或者简 ...
- string.Format()字符串格式化
Format()基本语法: {索引[,对齐][:格式字符串]} ·索引:表示引用的对象列表中的第n个对象参数. ·对齐(可选):设置宽度与对齐方式,该参数为带符号的整数.正数为 ...
- Oracle查询数据中占用空间最大的表
--第一步,查询istaudit数据库文件ID,文件路径 select file#,name from v$datafile where lower(name) like '%istaudit.dbf ...
- .html与.htm为网页后缀的区别
有些人在做网页的时候会有疑问,到底是应该用.html还是.htm做网页后缀呢?.html和.htm有什么区别吗?在做网页时到底用哪一个好呢? 现在,我说一下我对这两个文件后缀的看法: 首先,要想使用后 ...
- typeof操作符的返回值
使用typeof操作符 对一个值使用typeof操作符可能返回下列某个字符串: 1):undefined——如果这个值未定义 2):boolean——如果这个值是布尔值 3):string——如果这个 ...
- LNMP一键安装包sh脚本
Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...
- BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草
题目 1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 679 Solved: ...
- poj 1084 Brainman(归并排序)
题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...