欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于 List list = new ArrayList(int initialCapacity)

程序员文章站 2022-04-05 10:19:13
...
[url]http://wuaner.iteye.com/blog/460412[/url]

List list = new ArrayList(int initialCapacity); //构造一个初始容量为initialCapacity的空列表。    
Sysout.out.println(list.size()); //size()方法指的是List中实际存放有多少个元素,故这里是0


API中对ArrayList构造函数的解释:


ArrayList的部分源代码:

/**   
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list.
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
// 注意这里。默认长度为10。这回知道了吧
this(10);
}

/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list.
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
// 注意这里。默认长度为10。这回知道了吧
this(10);
}
相关标签: Blog