ArrayList--卧槽这就是源码
最近在《数据结构与算法分析(java版)》中看到了实现ArrayList的简易代码,亲自做了一下.个中的疑点还是在代码中实现了一下.其中就包括内部类Iterator对外部成员访问的问题.
private class MyIterator implements Iterator<AntType> {
private int current = 0;
@Override
public boolean hasNext() {
return current < size();
}
@Override
public AntType next() {
if(!hasNext())
throw new NoSuchElementException();
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
在14行有一个 theItems[current++],最好写法当然是MyArrayList.this.theItems[current++].
此后我又看了一下源代码,ArrayList中elementData的声明也是非常奇葩的:
transient Object[] elementData; // non-private to simplify nested class access
于是向百度寻找答案,说transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。说这个反正也不懂,继续往下看说用户的一些敏感信息(如密码,银行卡号等),为了安全起见,不希望在网络操作(主要涉及到序列化操作,本地序列化缓存也适用)中被传输,这些信息对应的变量就可以加上transient关键字。换句话说,这个字段的生命周期仅存于调用者的内存中而不会写到磁盘里持久化。
对于后面的注释
non-private to simplify nested class access
翻译过来就是"非私有化以简化嵌套类访问",
同时可以发现一下几个构造函数中elementData 都用的this.elementData
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
}
}
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
对于这种方式,就暂时归为严谨吧.第一段代码中elementData的定义使用的this.elementData = new Object[initialCapacity];成员类型为Object,这里就不用使用泛型也能代表任意类型,值得借鉴.
ArrayList--卧槽这就是源码的更多相关文章
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...
- Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 【转】Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
原文网址:http://www.cnblogs.com/skywang12345/p/3308556.html 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具 ...
- java基础解析系列(十)---ArrayList和LinkedList源码及使用分析
java基础解析系列(十)---ArrayList和LinkedList源码及使用分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder jav ...
- ArrayList详解-源码分析
ArrayList详解-源码分析 1. 概述 在平时的开发中,用到最多的集合应该就是ArrayList了,本篇文章将结合源代码来学习ArrayList. ArrayList是基于数组实现的集合列表 支 ...
- ArrayList、CopyOnWriteArrayList源码解析(JDK1.8)
本篇文章主要是学习后的知识记录,存在不足,或许不够深入,还请谅解. 目录 ArrayList源码解析 ArrayList中的变量 ArrayList构造函数 ArrayList中的add方法 Arra ...
- [数据结构1.2-线性表] 动态数组ArrayList(.NET源码学习)
[数据结构1.2-线性表] 动态数组ArrayList(.NET源码学习) 在C#中,存在常见的九种集合类型:动态数组ArrayList.列表List.排序列表SortedList.哈希表HashTa ...
- ArrayList & Vector的源码实现
#ArrayList & Vector #####前言: 本来按照计划,ArrayList和Vector是分开讲的,但是当我阅读了ArrayList和Vector的源码以后,我就改变了注意,把 ...
- ArrayList和LinkedList源码
1 ArrayList 1.1 父类 java.lang.Object 继承者 java.util.AbstractCollection<E> 继承者 java.util.Abstract ...
- ArrayList 和 LinkedList 源码分析
List 表示的就是线性表,是具有相同特性的数据元素的有限序列.它主要有两种存储结构,顺序存储和链式存储,分别对应着 ArrayList 和 LinkedList 的实现,接下来以 jdk7 代码为例 ...
随机推荐
- 记微信开发(自定义回复&关注回复)
记微信开发(自定义回复&关注回复) 记微信开发(自定义回复&关注回复) code: <?php/** * wechat php test *///define your toke ...
- Android反编译APK
http://blog.csdn.net/vipzjyno1/article/details/21039349/ 关于被加壳工具添加我的apk如何脱壳 http://www.blogfshare.co ...
- Composer的使用
安装 curl -sS https://getcomposer.org/installer | php 你可以使用--install-dir选项将Composer安装到指定的目录,例如: curl - ...
- 函数 - PHP手册笔记
用户自定义函数 函数无需在调用前被定义,除非是有条件定义的. PHP中的所有函数和类都具有全局作用域.PHP不支持函数重载,也不可能取消定义或者重定义已声明的函数. 特意试了下,我的电脑上的PHP递归 ...
- maven+springmvc+easyui+fastjson+pagehelper
1.maven配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- SQL Server dbcc shrinkfile 不起作用
方法 1.重建聚集索引. 方法 2.重建堆表. ---------------------------------------------------------------------------- ...
- progressbar样式
http://www.oschina.net/question/8676_11797 http://blog.csdn.net/ouyangtianhan/article/details/656576 ...
- JavaEE Tutorials (26) - 批处理
26.1批处理介绍391 26.1.1批处理作业中的步骤391 26.1.2并行处理392 26.1.3状态和判定元素392 26.1.4批处理框架功能39326.2Java EE中的批处理394 2 ...
- android安卓开发问题集 XMPP篇
1.消息推送查了下资料,后面还是使用了androidpn (1)java.security.KeyStoreException: KeyStore jks implementation not fou ...