C#中 ArrayList 的使用

來源:互聯網
上載者:User

標籤:city   trim   com   方法   清空   迴圈   test   comm   void   

using System.Collections; public class TestStudent {     public static void main(String args [])     {     //建立ArrayList對象     ArrayList students = new ArrayList();     //執行個體化幾個Student類對象     Student rose = new Student( "rose" ,25, "reading" );     Student jack = new Student( "jack" ,28, "singing" );     Student mimi = new Student( "mimi" ,26, "dancing" );     //利用ArrayList類的add()方法添加元素     students.add(rose);     students.add(jack);     students.add(mimi);     //利用ArrayList的Count屬性查看該集合中的元素數量     int number = students.Count;      Console.WriteLine( "共有元素" + number + "個" );     //讀取單個元素,因為存入ArrayList中的元素會變為Object類型,     //所以,在讀取時間,     Student stu = students[0] as Student;     stu.say();     //遍曆元素 -- 通過索引     for ( int i = 0;i < students.Count;i ++)     {      Student a = students[i] as Student;      a.say();     }     //利用foreach迴圈     foreach (Object o in students)     {       Student b = o as Student;       b.say();     }     //刪除元素 通過索引刪除     students.removeAt(0);     //刪除元素,  通過對象名     students.remove(jack);     //清空元素     students.Clear();     //我們知道,ArrayList的容量會隨著我們的需要自動按照一定規律     //進行填充,當我們確定不再添加元素時,我們要釋放多餘的空間     //這就用到了Capacity屬性和TrimtoSize()方法     //利用Capacity屬性可以查看當前集合的容量       //利用TrimtoSize()方法可以釋放多餘的空間       //查看當前容量     int number = students.Capacity;     //去除多餘的容量     students.TrimtoSize();     } }

C#中 ArrayList 的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.