Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
91ef656b
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
91ef656b
编写于
3月 24, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
d4ab9c37
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
16 addition
and
28 deletion
+16
-28
notes/Git.md
notes/Git.md
+2
-0
notes/Java 容器.md
notes/Java 容器.md
+14
-28
未找到文件。
notes/Git.md
浏览文件 @
91ef656b
...
...
@@ -154,3 +154,5 @@ $ ssh-keygen -t rsa -C "youremail@example.com"
<div
align=
"center"
>
<img
src=
"../pics//7a29acce-f243-4914-9f00-f2988c528412.jpg"
/>
</div><br>
比较详细的地址:http://www.cheat-sheets.org/saved-copy/git-cheat-sheet.pdf
notes/Java 容器.md
浏览文件 @
91ef656b
...
...
@@ -146,7 +146,7 @@ x.euqals(null); // false;
建议先阅读
[
算法-查找
](
https://github.com/CyC2018/Interview-Notebook/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E6%9F%A5%E6%89%BE
)
部分,对容器类源码的理解有很大帮助。
源码下载:
[
OpenJDK 1.7
](
http://download.java.net/openjdk/jdk7
)
以下源码属于 JDK 8,下载地址:
[
JDK-Source-Code
](
https://github.com/CyC2018/JDK-Source-Code
)
。
## ArrayList
...
...
@@ -158,31 +158,22 @@ x.euqals(null); // false;
```
java
public
class
ArrayList
<
E
>
extends
AbstractList
<
E
>
implements
List
<
E
>,
RandomAccess
,
Cloneable
,
java
.
io
.
Serializable
implements
List
<
E
>,
RandomAccess
,
Cloneable
,
java
.
io
.
Serializable
```
基于数组实现,保存元素的数组使用 transient 修饰,该关键字声明数组默认不会被序列化。这是 ArrayList 具有动态扩容特性,因此保存元素的数组不一定都会被使用,那么就没必要全部进行序列化。ArrayList 重写了 writeObject() 和 readObject() 来控制只序列化数组中有元素填充那么部分内容。
```
java
private
transient
Object
[]
elementData
;
transient
Object
[]
elementData
;
// non-private to simplify nested class access
```
数组的默认大小为 10。
```
java
public
ArrayList
(
int
initialCapacity
)
{
super
();
if
(
initialCapacity
<
0
)
throw
new
IllegalArgumentException
(
"Illegal Capacity: "
+
initialCapacity
);
this
.
elementData
=
new
Object
[
initialCapacity
];
}
public
ArrayList
()
{
this
(
10
);
}
private
static
final
int
DEFAULT_CAPACITY
=
10
;
```
删除元素时调用 System.arraycopy() 对元素进行复制,因此删除操作成本很高。
删除元素时
需要
调用 System.arraycopy() 对元素进行复制,因此删除操作成本很高。
```
java
public
E
remove
(
int
index
)
{
...
...
@@ -194,7 +185,7 @@ public E remove(int index) {
int
numMoved
=
size
-
index
-
1
;
if
(
numMoved
>
0
)
System
.
arraycopy
(
elementData
,
index
+
1
,
elementData
,
index
,
numMoved
);
elementData
[--
size
]
=
null
;
//
Let gc
do its work
elementData
[--
size
]
=
null
;
//
clear to let GC
do its work
return
oldValue
;
}
...
...
@@ -203,8 +194,9 @@ public E remove(int index) {
添加元素时使用 ensureCapacity() 方法来保证容量足够,如果不够时,需要使用 grow() 方法进行扩容,使得新容量为旧容量的 1.5 倍(oldCapacity + (oldCapacity >> 1))。扩容操作需要把原数组整个复制到新数组中,因此最好在创建 ArrayList 对象时就指定大概的容量大小,减少扩容操作的次数。
```
java
private
void
ensure
CapacityInternal
(
int
minCapacity
)
{
private
void
ensure
ExplicitCapacity
(
int
minCapacity
)
{
modCount
++;
// overflow-conscious code
if
(
minCapacity
-
elementData
.
length
>
0
)
grow
(
minCapacity
);
...
...
@@ -221,14 +213,6 @@ private void grow(int minCapacity) {
// minCapacity is usually close to size, so this is a win:
elementData
=
Arrays
.
copyOf
(
elementData
,
newCapacity
);
}
private
static
int
hugeCapacity
(
int
minCapacity
)
{
if
(
minCapacity
<
0
)
// overflow
throw
new
OutOfMemoryError
();
return
(
minCapacity
>
MAX_ARRAY_SIZE
)
?
Integer
.
MAX_VALUE
:
MAX_ARRAY_SIZE
;
}
```
### 2. Fail-Fast
...
...
@@ -238,17 +222,19 @@ modCount 用来记录 ArrayList 结构发生变化的次数。结构发生变化
在进行序列化或者迭代等操作时,需要比较操作前后 modCount 是否改变,如果改变了需要抛出 ConcurrentModificationException。
```
java
private
void
writeObject
(
java
.
io
.
ObjectOutputStream
s
)
throws
java
.
io
.
IOException
{
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
array length
s
.
writeInt
(
elementData
.
length
);
// 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
++)
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
s
.
writeObject
(
elementData
[
i
]);
}
if
(
modCount
!=
expectedModCount
)
{
throw
new
ConcurrentModificationException
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录