继续我的小激动~~~

1、看看构造一个ArrayList 有两种方式

一个指定大小,一个不指定。我们知道他其实使用数组来实现了,数组肯定要有大小,那么他没指定大小,默认的是多少呢???追踪源码---开启万里追踪模式~~~

   /**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this();
}

知道了,就是10~~。

2、还有一个通过现有的集合类来实现构造。怎么实现的啊???

   /**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
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);
}

原来是调用Arrays.copyOf() 这个工具类。

3、插入元素add()

  看起来好简单的方法,插入就行了呗。

诸位看官,有没有考虑,插不进去咋办?满了!~

    /**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}

int newCapacity = oldCapacity + (oldCapacity >> 1); 

动态扩容,很显然变为了原来的1.5倍。【在JDK6中 int newCapacity = oldCapacity *3/2+1;】

4、查找 indexOf()

  其实还不是一个一个的遍历!!但是不要忘了咱们的ArrayList可以存null的~

  这就要分类了,看看源码的

    /**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

  如果是null 就遍历所有的,使用==

  如果不是,还要遍历 使用equals

  【从这里我们就知道了,二者是不一样的】哇塞!~

5、clone / toArray()

  怎么克隆?? 还是Arrays.copyOf()!!!

6、在指定位置插入元素 add(int index, E elements)

  咋个实现?挪呗!~index 后面的元素,乖乖的向后走一个,给我个地方~!

   public void add(int index, E element) {
rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

  现在知道删除remove(index)是如何实现的了吧?但是挪了之后,最后一个元素怎么处理??置null让gc自己处理。不信自己看源码!~

  那么,删除指定元素呢?(在我们ArrayList中指的是删除第一个出现的元素。)如果这个元素是null ? 你可依然记得indexOf()里面怎么实现的(== 和 equals) ,yes!还是分两种情况查找,删除!

7、clear()

  全部元素置null

这个ArrayList 就是这么多了 ~ 么么嗒

转载请注明:http://home.cnblogs.com/u/plxx/

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

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

    今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的.所以笔者决定首先将从一些容器下手.[ ...

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

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

  3. ansible笔记(8):常用模块之系统类模块(二)

    ansible笔记():常用模块之系统类模块(二) user模块 user模块可以帮助我们管理远程主机上的用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 此处我们介绍一些user模块 ...

  4. 101 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 05 通过方法实现学生类与专业类关联——方案二

    101 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 05 通过方法实现学生类与专业类关联--方案二 本文知识点:通过方法实现学生类与 ...

  5. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

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

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

  7. C++中的类和对象(二)

    一,对象的动态建立和释放 1.什么是对象的动态建立和释放 通常我们创建的对象都是由C++编译器为我们在栈内存中创建的,我们无法对其进行生命周期的管理.所以我们需要动态的去建立该对象,因此我们需要在堆内 ...

  8. 深入理解Java常用类----String(二)

    上篇介绍了String类的构造器,获取内部属性等方法,最后留下了最常用的局部操作函数没有介绍,本篇将接着上篇内容,从这些最常见的函数的操作说起,看看我们日常经常使用的这些方法的内部是怎么实现的.第一个 ...

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

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

随机推荐

  1. U盘安装Ubuntu14.4时遇到分区问题记录

    1.在安装Ubuntu14.4时,遇到如果先分出 / 跟挂载的主分区时,后面只能再分一个swap,或者挂载一个/home,或者一个/ boot 时不能继续分区,当然想安装也是不能不能成功的. 解决办法 ...

  2. use MSBuild cmd to build and deploy the database project.

    You can use the below command to implement it. SET TargetDBName="Database name" SET DataBa ...

  3. iOS推送介绍

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

  4. poj 1715 Hexadecimal Numbers 排列组合

    /** 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(1 ...

  5. ASP.NET PipeLine #Reprinted#

    从ASP.NET 1.0 起,相比于ASP中的COM, PipeLine 就是一项重大的改进. ASP.NET 时代的管道模型究竟是怎么样的? 我们能接触到的四个最重要的概念就是HttpApplica ...

  6. Delphi 编写系统服务(服务控制线程:开始,暂停,继续,停止)

    打开Delphi 7,新建,选择Other->Service Application 修改属性中Name和DisplayName,Name是Service的名称,决定着进程的标识,Display ...

  7. Niagara技术文档汇总

    Niagara技术文档汇总http://wenku.baidu.com/view/ccdd4e2c3169a4517723a38f.html Niagara讲解要点http://wenku.baidu ...

  8. 基于visual Studio2013解决C语言竞赛题之0303最大数

     题目 解决代码及点评 这道题考察对条件分支和赋值的灵活应用 正常思维 如果 a>b and a>c 那么a最大 如果b>c and b>a 那么b最大 如果c>a ...

  9. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  10. [Android学习笔记5]四大应用组件之一:Service 下

    绑定方式的Service使用 在实现绑定服务时,最重要的是定义onBind()回调方法返回的接口,有三种方式: 1. 继承Binder类 2. 使用Messenger 3. 使用AIDL 这里对1,2 ...