一天一个类--ArrayList之二
继续我的小激动~~~
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 ? get(i)==null : 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之二的更多相关文章
- 一天一个类--ArrayList之一
今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的.所以笔者决定首先将从一些容器下手.[ ...
- java 集合之实现类ArrayList 和 LinkedList
List 的方法列表 方法名 功能说明 ArrayList() 构造方法,用于创建一个空的数组列表 add(E e) 将指定的元素添加到此列表的尾部 get(int index) 返回此列表中指定位置 ...
- ansible笔记(8):常用模块之系统类模块(二)
ansible笔记():常用模块之系统类模块(二) user模块 user模块可以帮助我们管理远程主机上的用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 此处我们介绍一些user模块 ...
- 101 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 05 通过方法实现学生类与专业类关联——方案二
101 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 05 通过方法实现学生类与专业类关联--方案二 本文知识点:通过方法实现学生类与 ...
- Spring源码分析——BeanFactory体系之抽象类、类分析(二)
上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...
- java的List接口的实现类 ArrayList,LinkedList,Vector 的区别
Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. Array ...
- C++中的类和对象(二)
一,对象的动态建立和释放 1.什么是对象的动态建立和释放 通常我们创建的对象都是由C++编译器为我们在栈内存中创建的,我们无法对其进行生命周期的管理.所以我们需要动态的去建立该对象,因此我们需要在堆内 ...
- 深入理解Java常用类----String(二)
上篇介绍了String类的构造器,获取内部属性等方法,最后留下了最常用的局部操作函数没有介绍,本篇将接着上篇内容,从这些最常见的函数的操作说起,看看我们日常经常使用的这些方法的内部是怎么实现的.第一个 ...
- java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析
java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...
随机推荐
- hdu 5007 水 弦
http://acm.hdu.edu.cn/showproblem.php?pid=5007 纯粹的联系String的substr 什么时候substr拦截比写短话 string te; int n; ...
- SSIS 连接 PostgreSQL
因为工作需要,得把psql的表放到SQL Server, 找到一个PGNP(http://www.pgoledb.com/) 的适配器,不过一看要300$就没有去尝试了. 官方倒是有ODBC的驱动. ...
- 加特殊符号星号斜杠反斜杠/* \ */ !important等让css实现兼容各个浏览器的技巧的代码
在编写css样式表的时候常常会碰到一写浏览器兼容的问题,象是不同内核的浏览器显示就不一定相同,不同版本的的浏览器也会产生上下兼容的问题,如何解决这些问题成了我们苦恼的问题,如果你对css hac ...
- UVa----------1594(Ducci Sequence)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- week4_motion_of_ball_1(小球运动)——改进
# Ball motion with an explicit timer import simplegui # Initialize globals width = 600 height = 600 ...
- hdu 4782 Beautiful Soupz
模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...
- Laravel OAuth2 (一) ---简单获取用户信息
前言 本来要求是使用微信进行第三方登陆,所以想着先用 github 测试成功再用微信测试,可是最近拖了好久都还没申请好微信开放平台的 AppID ,所以就只写 github 的第三方登陆吧,估计微信的 ...
- Zoj 3842 Beauty of Array
Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...
- java学习之反射
package com.gh.ref; public class Person { private String name; private int age; private char sex; pr ...
- iOS UIView非常用方法及属性详解
在调用视图的 Quartz 调用都可以正确地在视图中描画. 视图对象通过 bounds .和 center 属 性声明来跟踪自己的大小和位置.frame 属性包含一个矩形,即边框矩形,用于指定 ...