Skip to content

Commit bd5049f

Browse files
committed
添加了java集合的部分笔记
1 parent 2b8faf2 commit bd5049f

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

java/basic/java-basic.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,13 @@ System.out.println(InterfaceExample.x);
602602
- [深入理解 abstract class 和 interface](https://www.ibm.com/developerworks/cn/java/l-javainterface-abstract/)
603603
- [When to Use Abstract Class and Interface](https://dzone.com/articles/when-to-use-abstract-class-and-intreface)
604604

605+
### Java中还有一个叫标记接口(接口中没有任何方法,如Cloneable, RandomAccess, Serializable)
606+
标记接口(Marker):这就说明了 RandomAccess 为空的原因,这个接口的功能仅仅起到标记的作用。
607+
这不是与序列化接口 Serializable 差不多吗? 只要你认真观察, 其实不只这一个标记接口, 实际上 ArrayList 还实现了另外两个这样的空接口:
608+
Cloneable 接口 :实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制。 如果在没有实现 Cloneable 接口的实例上调用 Object 的 clone 方法,则会导致抛出 CloneNotSupportedException 异常。
609+
Serializable 接口: 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
610+
611+
605612

606613
## super
607614

java/basic/java-collection.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ List list = Arrays.asList(1, 2, 3);
115115

116116
### 1. 概览
117117

118-
因为 ArrayList 是基于数组实现的,所以支持快速随机访问。RandomAccess 接口标识着该类支持快速随机访问。
118+
因为 ArrayList 是基于数组实现的,所以支持快速随机访问(可以使用下标来访问,随机指的是下标是随机的 get(int index))。RandomAccess 接口标识着该类支持快速随机访问。
119+
120+
ArrayList类中存在两个私有成员(当然不仅仅只有两个),elementData是一个Object类型的数组,用来保存元素;size表示ArrayList中实际的元素数目(所以size()方法返回的是实际元素的数目)。
119121

120122
```java
121123
public class ArrayList<E> extends AbstractList<E>
@@ -126,6 +128,10 @@ public class ArrayList<E> extends AbstractList<E>
126128

127129
```java
128130
private static final int DEFAULT_CAPACITY = 10;
131+
132+
public ArrayList() {
133+
this(10);
134+
}
129135
```
130136

131137
<div align="center"> <img src="pics/52a7744f-5bce-4ff3-a6f0-8449334d9f3d.png" width="400px"> </div><br>
@@ -170,6 +176,9 @@ private void grow(int minCapacity) {
170176
}
171177
```
172178

179+
看了这几段代码,ensureCapacity(minCapacity)的作用就显而易见了,就是增加数组列表的容量,原理很简单,首先判断该参数是否大于0并且是否大于当前容量,然后就到了grow方法,其他的语句先别管,看最后一句,elementData = Arrays.copyOf(elementData, newCapacity); 这条语句先按指定参数创建一个更大的数组,然后把原数组的元素一个个拷贝到大数组中,最后将elementData 的引用指向这个大数组。这样,就增大了数组列表的容量,而小数组也会被gc回收。所以数据量很大的时候还是建议初始化的时候就指定容量,提高效率
180+
181+
173182
### 3. 删除元素
174183

175184
需要调用 System.arraycopy() 将 index+1 后面的元素都复制到 index 位置上,该操作的时间复杂度为 O(N),可以看出 ArrayList 删除元素的代价是非常高的。
@@ -545,7 +554,7 @@ private V putForNullKey(V value) {
545554
```java
546555
void addEntry(int hash, K key, V value, int bucketIndex) {
547556
if ((size >= threshold) && (null != table[bucketIndex])) {
548-
resize(2 * table.length);
557+
resize(2 * table.length); // resize的时候double size
549558
hash = (null != key) ? hash(key) : 0;
550559
bucketIndex = indexFor(hash, table.length);
551560
}
@@ -774,7 +783,7 @@ static final int tableSizeFor(int cap) {
774783
### 9. 与 HashTable 的比较
775784

776785
- HashTable 使用 synchronized 来进行同步。
777-
- HashMap 可以插入键为 null 的 Entry。
786+
- HashMap 可以插入键为 null 的 Entry,HashTable不可以
778787
- HashMap 的迭代器是 fail-fast 迭代器。
779788
- HashMap 不能保证随着时间的推移 Map 中的元素次序是不变的。
780789

0 commit comments

Comments
 (0)