Java基础随笔1

最近准备跳槽了,可能要从安卓转做javaweb,抓紧时间复习一下java的基础内容。正好在github上发现了crossoverJie的Java-Interview项目,就来逐项学习一下。

————————————————————————————————————————————————————————————————————————————

ArrayList/Vector 的底层分析

ArrayList

ArrayList 实现于 ListRandomAccess 接口。可以插入空数据,也支持随机访问。

ArrayList相当于动态数据,其中最重要的两个属性分别是: elementData 数组,以及 size 大小。 在调用 add() 方法的时候:

    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!! elementData[size++] = e; return true; }
  • 首先进行扩容校验。
  • 将插入的值放到尾部,并将 size + 1 。

如果是调用 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++; }
  • 也是首先扩容校验。
  • 接着对数据进行复制,目的是把 index 位置空出来放本次插入的数据,并将后面的数据向后移动一个位置。

其实扩容最终调用的代码:

    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 自定义了序列化与反序列化:

    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++) { 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++) { a[i] = s.readObject(); } } }

当对象中自定义了 writeObject 和 readObject 方法时,JVM 会调用这两个自定义方法来实现序列化与反序列化。

从实现中可以看出 ArrayList 只序列化了被使用的数据。

以上为复制git的内容。下面解释一下其中我不太熟悉和明白的内容。

————————————————————————————————————————————————————————————

一、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等方法是执行的简单的循环来查找元素,会造成效率低下的问题,还不如自己手写算法进行查找。

二、List接口

List接口中可以存放任意的数据。而且在List接口中内容是允许重复的。List接口的功能要比Collection接口强大很多,因为大量扩充了Collection接口的操作。
扩展方法:
1、public void add(int index,E element)普通  在指定的位置增加元素。
2、public boolean addAll(int index, Collection c)  普通 在指定的位置增加一组元素。
3、E get(int index) 普通 返回指定位置的元素。
4、public int indexOf(Object o) 普通 查找指定元素的位置。
5、public int lastIndexOf(Object o) 普通 从后向前查找指定元素的位置。
6、public ListIterator listIterator()普通 为ListIterator接口实例化。
7、public E remove(int index) 普通 按指定的位置删除元素。
8、public List subList(int fromIndex, int toIndex) 普通 取出集合中的子集合。
9、public E set(int index, E element)普通 替换指定位置的元素。
 
三、RandomAccess接口
RandomAccess 是一个标记接口,用于标明实现该接口的List支持快速随机访问,主要目的是使算法能够在随机和顺序访问的list中表现的更加高效。
 

Vector

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中的操作是线程安全的
 
 

 

转载于:https://www.cnblogs.com/Aurel1ano/p/9160380.html

你可能感兴趣的:(Java基础随笔1)