初始化ArrayList集合的方式有幾種?可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、做網(wǎng)站、莆田網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、莆田網(wǎng)絡(luò)營銷、莆田企業(yè)策劃、莆田品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供莆田建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:chinadenli.net
概述
ArrayList是一個以動態(tài)數(shù)組為基礎(chǔ)實(shí)現(xiàn)的非線程安全的集合,ArrayList的元素可以為空、可以重復(fù),同時又是有序的(讀取和存放的順序一致 )。
ArrayList繼承AbstractList,實(shí)現(xiàn)了List、RandomAccess(可以快速訪問)、Cloneable(可以被克隆)、java.io.Serializable(支持序列化)
ArrayList的初始化方式有三種:
1、無參構(gòu)造,默認(rèn)長度為10,是我們使用的最多的一種初始化方式:
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}這個時候,我們從源碼中可以看到,里面只有一行代碼:this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA,那么定義的DEFAULTCAPACITY_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 = {};通過注釋可以得知,源碼中定義了一個空的數(shù)組作為默認(rèn)的大小,并且在第一個元素添加進(jìn)來的時候再確定把數(shù)組擴(kuò)充多少,這段邏輯會在接下來添加元素部分作出解釋。
2、指定初始化長度:
/**
* 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
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
}
}3、用一個Collection對象來構(gòu)造
/**
* 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<? extends E> c) {
elementData = c.toArray();
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;
}
}看完上述內(nèi)容,你們對初始化ArrayList集合的方式有進(jìn)一步的了解嗎?如果還想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。
網(wǎng)頁名稱:初始化ArrayList集合的方式有幾種
分享地址:http://chinadenli.net/article40/jhgsho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站收錄、電子商務(wù)、網(wǎng)站排名、標(biāo)簽優(yōu)化、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)