Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
7e850df0
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7e850df0
编写于
4月 29, 2014
作者:
M
mduigou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8035584: ArrayList(c) should avoid inflation if c is empty
Reviewed-by: martin
上级
3ec1c1d7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
32 addition
and
17 deletion
+32
-17
src/share/classes/java/util/ArrayList.java
src/share/classes/java/util/ArrayList.java
+32
-17
未找到文件。
src/share/classes/java/util/ArrayList.java
浏览文件 @
7e850df0
...
...
@@ -118,11 +118,18 @@ public class ArrayList<E> extends AbstractList<E>
*/
private
static
final
Object
[]
EMPTY_ELEMENTDATA
=
{};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private
static
final
Object
[]
DEFAULTCAPACITY_EMPTY_ELEMENTDATA
=
{};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData ==
EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
* empty ArrayList with elementData ==
DEFAULTCAPACITY_EMPTY_ELEMENTDATA
*
will be expanded to
DEFAULT_CAPACITY when the first element is added.
*/
transient
Object
[]
elementData
;
// non-private to simplify nested class access
...
...
@@ -141,19 +148,21 @@ public class ArrayList<E> extends AbstractList<E>
* is negative
*/
public
ArrayList
(
int
initialCapacity
)
{
super
();
if
(
initialCapacity
<
0
)
if
(
initialCapacity
>
0
)
{
this
.
elementData
=
new
Object
[
initialCapacity
];
}
else
if
(
initialCapacity
==
0
)
{
this
.
elementData
=
EMPTY_ELEMENTDATA
;
}
else
{
throw
new
IllegalArgumentException
(
"Illegal Capacity: "
+
initialCapacity
);
this
.
elementData
=
new
Object
[
initialCapacity
];
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public
ArrayList
()
{
super
();
this
.
elementData
=
EMPTY_ELEMENTDATA
;
this
.
elementData
=
DEFAULTCAPACITY_EMPTY_ELEMENTDATA
;
}
/**
...
...
@@ -166,10 +175,14 @@ public class ArrayList<E> extends AbstractList<E>
*/
public
ArrayList
(
Collection
<?
extends
E
>
c
)
{
elementData
=
c
.
toArray
();
size
=
elementData
.
length
;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if
(
elementData
.
getClass
()
!=
Object
[].
class
)
elementData
=
Arrays
.
copyOf
(
elementData
,
size
,
Object
[].
class
);
if
((
size
=
elementData
.
length
)
!=
0
)
{
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if
(
elementData
.
getClass
()
!=
Object
[].
class
)
elementData
=
Arrays
.
copyOf
(
elementData
,
size
,
Object
[].
class
);
}
else
{
// replace with empty array.
this
.
elementData
=
EMPTY_ELEMENTDATA
;
}
}
/**
...
...
@@ -180,7 +193,9 @@ public class ArrayList<E> extends AbstractList<E>
public
void
trimToSize
()
{
modCount
++;
if
(
size
<
elementData
.
length
)
{
elementData
=
Arrays
.
copyOf
(
elementData
,
size
);
elementData
=
(
size
==
0
)
?
EMPTY_ELEMENTDATA
:
Arrays
.
copyOf
(
elementData
,
size
);
}
}
...
...
@@ -192,11 +207,11 @@ public class ArrayList<E> extends AbstractList<E>
* @param minCapacity the desired minimum capacity
*/
public
void
ensureCapacity
(
int
minCapacity
)
{
int
minExpand
=
(
elementData
!=
EMPTY_ELEMENTDATA
)
// any size if
real
element table
int
minExpand
=
(
elementData
!=
DEFAULTCAPACITY_
EMPTY_ELEMENTDATA
)
// any size if
not default
element table
?
0
// larger than default for
empty table. It's already supposed to be
// at default size.
// larger than default for
default empty table. It's already
//
supposed to be
at default size.
:
DEFAULT_CAPACITY
;
if
(
minCapacity
>
minExpand
)
{
...
...
@@ -205,7 +220,7 @@ public class ArrayList<E> extends AbstractList<E>
}
private
void
ensureCapacityInternal
(
int
minCapacity
)
{
if
(
elementData
==
EMPTY_ELEMENTDATA
)
{
if
(
elementData
==
DEFAULTCAPACITY_
EMPTY_ELEMENTDATA
)
{
minCapacity
=
Math
.
max
(
DEFAULT_CAPACITY
,
minCapacity
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录