关于 List list = new ArrayList(int initialCapacity)
程序员文章站
2022-04-05 10:19:13
...
[url]http://wuaner.iteye.com/blog/460412[/url]
API中对ArrayList构造函数的解释:
ArrayList的部分源代码:
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);
}
上一篇: win10系统安装声卡驱动失败怎么解决
下一篇: 用方法对象取代方法
推荐阅读
-
Java中res.add(list)和res.add(new ArrayList<Integer>(list))的区别
-
Java 初始化集合(List)的方式汇总,new ArrayList()
-
Arrays.asList返回的List与new ArrayList的区别
-
关于java数组与ArrayList、List的浅谈
-
Java 初始化集合(List)的方式汇总,new ArrayList()
-
Java中res.add(list)和res.add(new ArrayList<Integer>(list))的区别
-
关于 List list = new ArrayList(int initialCapacity)
-
Java 面试题之反射的应用(如何在List<String> list = new ArrayList<String>();中放入Integer类型的数据)