編程經驗:List轉換成為ArrayList__編程

來源:互聯網
上載者:User

ArrayList 類

集合ArrayList包含的元素可以數字,字串,object等等,ArrayList 的容量是 ArrayList 可以儲存的元素數,隨著向 ArrayList 中添加元素,容量通過重新分配按需自動增加。可通過調用 TrimToSize 或通過顯式設定 Capacity 屬性減少容量。

使用整數索引可以訪問此集合中的元素。 此集合中的索引從零開始。 ArrayList 接受 null 作為有效值並且允許重複的元素。

List轉換成為ArrayList。(這裡的List是實體是ArrayList)

 

1、泛型轉換

調用ArrayList的AddRange方法,

 

List<ClassTreeInfo> list = new List<ClassTreeInfo>;

ArrayList arrayList = new ArrayList();

arrayList.AddRange(list);

 

這裡的ArrayList等價於List(結構和內容是一樣的)

 

2、非泛型轉換

這個需要遍曆List迴圈加入到ArrayList。

List<ClassTreeInfo> list = new List<ClassTreeInfo>;

ArrayList arrayList = new ArrayList();

arrayList.Add(list[i].id) ;

 

這裡的ArrayList僅僅是個int數組,儲存list裡面的id欄位的資料而已。

更多詳情請關注太陽灣:http://www.rrbay.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.