博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java之ArrayList集合
阅读量:7164 次
发布时间:2019-06-29

本文共 1462 字,大约阅读时间需要 4 分钟。

hot3.png

创建一个ArrayList的3个构造方法

/**     * Constructs an empty list with an initial capacity of ten.     */	 //方法一 有指定大小,默认size为10    public ArrayList() {        this(10);    }	/**     * Constructs an empty list with the specified initial capacity.     *     * @param  initialCapacity  the initial capacity of the list     * @throws IllegalArgumentException if the specified initial capacity     *         is negative     */	 // 方法二 指定size的大小    public ArrayList(int initialCapacity) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+                                               initialCapacity);        this.elementData = new Object[initialCapacity];    }		// 方法3,直接传入一个Collection的集合	/**     * Constructs a list containing the elements of the specified     * collection, in the order they are returned by the collection's     * iterator.     *     * @param c the collection whose elements are to be placed into this list     * @throws NullPointerException if the specified collection is null     */    public ArrayList(Collection
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); }

实用中几乎都是List list = new ArrayList()这样了吧。

转载于:https://my.oschina.net/u/2301293/blog/1927552

你可能感兴趣的文章