List轉換成為ArrayList


List轉換成為ArrayList

 
 

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字段的數據而已。

版權聲明:本文為博主原創文章,未經博主允許不得轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM