最近准备跳槽了,可能要从安卓转作javaweb,抓紧时间复习一下java的基础内容。正好在github上发现了crossoverJie的Java-Interview项目,就来逐项学习一下。java
————————————————————————————————————————————————————————————————————————————git
ArrayList
实现于 List
、RandomAccess
接口。能够插入空数据,也支持随机访问。github
ArrayList
至关于动态数据,其中最重要的两个属性分别是: elementData
数组,以及 size
大小。 在调用 add()
方法的时候:web
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
若是是调用 add(index,e)
在指定位置添加的话:算法
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++; }
其实扩容最终调用的代码:数组
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); }
也是一个数组复制的过程。安全
因而可知 ArrayList
的主要消耗是数组扩容以及在指定位置添加数据,在平常使用时最好是指定大小,尽可能减小扩容。更要减小在指定位置插入数据的操做。数据结构
因为 ArrayList 是基于动态数组实现的,因此并非全部的空间都被使用。所以使用了 transient
修饰,能够防止被自动序列化。并发
transient Object[] elementData;
所以 ArrayList 自定义了序列化与反序列化:dom
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioural compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. //只序列化了被使用的数据 for (int i=0; i<size; i++) { s.writeObject(elementData[i]); } if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) { // be like clone(), allocate array based upon size not capacity ensureCapacityInternal(size); Object[] a = elementData; // Read in all elements in the proper order. for (int i=0; i<size; i++) { a[i] = s.readObject(); } } }
当对象中自定义了 writeObject 和 readObject 方法时,JVM 会调用这两个自定义方法来实现序列化与反序列化。
从实现中能够看出 ArrayList 只序列化了被使用的数据。
以上为复制git的内容。下面解释一下其中我不太熟悉和明白的内容。
————————————————————————————————————————————————————————————
1、ArrayList
首先是ArrayList基础,ArrayList指的就是动态数组,包括三个主要的优势:
动态的增长和减小元素
实现了ICollection和IList接口
灵活的设置数组的大小
ArrayList有三个构造器,分别是public ArrayList(); public ArrayList(ICollection); public ArrayList(int); (第一个构造器默认按照16的大小构造,第二个将该集合的元素添加到ArrayList,第三个按照参数构造)。
IsSynchronized属性指示当前的ArrayList实例是否支持线程同步,而ArrayList.Synchronized静态方法则会返回一个ArrayList的线程同步的封装。 (先放这,线程问题后面再详细解释)
TrimSize方法用于将ArrayList固定到实际元素的大小,当动态数组元素肯定不在添加的时候,能够调用这个方法来释放空余的内存。
效率问题:
1.数组扩容是对ArrayList效率影响比较大的一个因素。 每当执行Add、AddRange、Insert、InsertRange等添加元素的方法,都会检查内部数组的容量是否不够了,会以当前容量的两倍来从新构建一个数组,将旧元素Copy到新数组中,而后丢弃旧数组。若是不运行TrimSize方法或者存储元素不足填满,会形成较大的内存浪费。
2.调用IndexOf、Contains等方法是执行的简单的循环来查找元素,会形成效率低下的问题,还不如本身手写算法进行查找。
2、List接口
Voctor
也是实现于 List
接口,底层数据结构和 ArrayList
相似,也是一个动态数组存放数据。不过是在 add()
方法的时候使用 synchronize
进行同步写数据,可是开销较大,因此 Vector
是一个同步容器并非一个并发容器。
如下是 add()
方法:
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; }
以及指定位置插入数据:
public void add(int index, E element) { insertElementAt(element, index); } public synchronized void insertElementAt(E obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; }
_________________________________________________________________________________
除此以外,再详细介绍一个Vector这个神奇的东西。
Vector 是矢量队列,它是JDK1.0版本添加的类。继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口。
Vector 继承了AbstractList,实现了List;因此,它是一个队列,支持相关的添加、删除、修改、遍历等功能。
Vector 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,咱们便可以经过元素的序号快速获取元素对象;这就是快速随机访问。
Vector 实现了Cloneable接口,即实现clone()函数。它能被克隆。
和ArrayList不一样,Vector中的操做是线程安全的。