牛骨文教育服务平台(让学习变的简单)
博文笔记

Java ArrayList 初始化

创建时间:2015-08-06 投稿人: 浏览次数:2554

List thisRow = new ArrayList(10);

java.util.ArrayList.ArrayList(int initialCapacity)

ArrayList
public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
Parameters:
initialCapacity - the initial capacity of the list
Throws:
IllegalArgumentException - if the specified initial capacity is negative

这个构造方法是建立了初始容量为initialCapacity的List, 但它此时为空, size()是0.

import java.util.ArrayList;
import java.util.List;

public class ValidAnagram {
    public static void main(String[] args) {
        List<Integer> thisRow = new ArrayList<Integer>(10);
        System.out.println(thisRow.size());

        thisRow.add(1);
        System.out.println(thisRow.size());
    }
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。