diff --git a/Java-Interview/.travis.yml b/Java-Interview/.travis.yml new file mode 100644 index 0000000..9396aad --- /dev/null +++ b/Java-Interview/.travis.yml @@ -0,0 +1,9 @@ +language: java + +install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true +script: mvn -DskipTests=true clean install + + +branches: + only: + - master \ No newline at end of file diff --git a/Java-Interview/LICENSE b/Java-Interview/LICENSE new file mode 100644 index 0000000..f74ee34 --- /dev/null +++ b/Java-Interview/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 crossoverJie + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Java-Interview/MD/ArrayList.md b/Java-Interview/MD/ArrayList.md new file mode 100644 index 0000000..8601cd1 --- /dev/null +++ b/Java-Interview/MD/ArrayList.md @@ -0,0 +1,151 @@ +# ArrayList/Vector 的底层分析 + +## ArrayList + +`ArrayList` 实现于 `List`、`RandomAccess` 接口。可以插入空数据,也支持随机访问。 + +`ArrayList `相当于动态数据,其中最重要的两个属性分别是: +`elementData` 数组,以及 `size` 大小。 +在调用 `add()` 方法的时候: +```java + public boolean add(E e) { + ensureCapacityInternal(size + 1); // Increments modCount!! + elementData[size++] = e; + return true; + } +``` + +- 首先进行扩容校验。 +- 将插入的值放到尾部,并将 size + 1 。 + +如果是调用 `add(index,e)` 在指定位置添加的话: +```java + 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 位置空出来放本次插入的数据,并将后面的数据向后移动一个位置。 + +其实扩容最终调用的代码: +```java + 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` 修饰,可以防止被自动序列化。 + +```java +transient Object[] elementData; +``` + +因此 ArrayList 自定义了序列化与反序列化: + +```java + 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