Collection

ArrayList

(1)一種非泛型的list
ArrayList is a non-generic list that accepts any Object type for its elements.
(2)其長度可根據(jù)添加的數(shù)量變動
Using the default constructor creates an empty list. As soon as elements are added to the list, the capacity of the list is extended to allow 4 elements. If the fifth element is added, the list is resized to allow 8 elements. If 8 elements are not enough, the list is resized again to contain 16 elements. With every resize the capacity of the list is doubled.
(3)長度改變的原理
When the capacity of the list changes, the complete collection is reallocated to a new memory block. With the implementation of List<T>, an array of type T is used. With reallocation, a new array is created, and Array.Copy copies the elements from the old array to the new array. To save time, if you know the number of elements in advance, that should be in the list; you can define the capacity with the constructor.
當(dāng)長度改變時雳刺,重新分配一塊內(nèi)存闻蛀,創(chuàng)建一個新的array见咒,舊的array被copy
(4)兩個屬性:Capacity和Count
1)Capacity:容量
You can get and set the capacity of a collection by using the Capacity property.
eg:
List<int> intList = new List<int>(10);
intList.Capacity = 20;
2)Count:元素個數(shù)
The number of elements in the collection can be read with the Count property.
eg:
intList.Count
(5)TrimExcess:可以去掉一些用不到的
If you are finished adding elements to the list and don’t want to add any more, you can get rid of the unneeded capacity by invoking the TrimExcess method; however, because the relocation takes time, TrimExcess has no effect if the item count is more than 90 percent of capacity.
eg:
intList.TrimExcess();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末季希,一起剝皮案震驚了整個濱河市戚哎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖澜共,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異锥腻,居然都是意外死亡嗦董,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進(jìn)店門瘦黑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來京革,“玉大人,你說我怎么就攤上這事幸斥∑ヒ。” “怎么了?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵睡毒,是天一觀的道長来惧。 經(jīng)常有香客問我冗栗,道長演顾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任隅居,我火速辦了婚禮钠至,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘胎源。我一直安慰自己棉钧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布涕蚤。 她就那樣靜靜地躺著宪卿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪万栅。 梳的紋絲不亂的頭發(fā)上佑钾,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天,我揣著相機(jī)與錄音烦粒,去河邊找鬼休溶。 笑死,一個胖子當(dāng)著我的面吹牛扰她,可吹牛的內(nèi)容都是我干的兽掰。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼徒役,長吁一口氣:“原來是場噩夢啊……” “哼孽尽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起忧勿,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤杉女,失蹤者是張志新(化名)和其女友劉穎艇拍,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宠纯,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卸夕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了婆瓜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片快集。...
    茶點(diǎn)故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖廉白,靈堂內(nèi)的尸體忽然破棺而出个初,到底是詐尸還是另有隱情,我是刑警寧澤猴蹂,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布院溺,位于F島的核電站,受9級特大地震影響磅轻,放射性物質(zhì)發(fā)生泄漏珍逸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一聋溜、第九天 我趴在偏房一處隱蔽的房頂上張望谆膳。 院中可真熱鬧,春花似錦撮躁、人聲如沸漱病。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杨帽。三九已至,卻和暖如春嗤军,著一層夾襖步出監(jiān)牢的瞬間注盈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工型雳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留当凡,地道東北人。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓纠俭,卻偏偏與公主長得像沿量,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子冤荆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評論 2 361