C#動態數組ArrayList介紹

來源:互聯網
上載者:User

ArrayList是一種動態數組,其容量可隨著我們的需要自動進行擴充.

ArrayList位於System.Collections命名空間中,所以我們在使用時,需要匯入此命名空間.

下面,我們還是在Student類的基礎上利用ArrayList操作,從而瞭解ArrayList的用法

Code:
  1. public class Student   
  2. {   
  3.               public Student(){}   
  4.   
  5.               public Student(String name,int age,String hobby)   
  6.               {   
  7.                            this.Name = name;   
  8.                            this.Age = age;   
  9.                            this.Hobby = hobby;   
  10.               }   
  11.   
  12.               private String name;   
  13.               public String Name   
  14.              {   
  15.                      get{return name;}   
  16.                      set{name = value;}   
  17.              }   
  18.   
  19.              private int age;   
  20.              public int Age   
  21.             {   
  22.                      get{return age;}   
  23.                      set{age = value;}   
  24.             }   
  25.   
  26.             private String hobby;   
  27.             public String Hobby   
  28.            {   
  29.                      get{return hobby;}   
  30.                      set{hobby = value;}   
  31.            }   
  32.   
  33.             public void say()   
  34.             {   
  35.                      Console.WriteLine("大家好,我是'{0}',今年{1}歲,我喜歡'{2}'",   
  36.                       this.Name,this.Age,this.Hobby);   
  37.             }   
  38. }  

編寫測試類別,瞭解ArrayList的方法

Code:
  1. using System.Collections;   
  2.   
  3. public class TestStudent   
  4. {   
  5.               public static void main(String args [])   
  6.              {   
  7.                          //建立ArrayList對象   
  8.            ArrayList students = new ArrayList();   
  9.   
  10.                          //執行個體化幾個Student類對象   
  11.            Student rose = new Student("rose",25,"reading");   
  12.                           Student jack = new Student("jack",28,"singing");   
  13.                           Student mimi = new Student("mimi",26,"dancing");   
  14.   
  15.                           //利用ArrayList類的add()方法添加元素   
  16.            students.add(rose);   
  17.                           students.add(jack);   
  18.                           students.add(mimi);   
  19.   
  20.                           //利用ArrayList的Count屬性查看該集合中的元素數量   
  21.            int number = students.Count;   
  22.                           Console.WriteLine("共有元素" + number + "個");   
  23.   
  24.                           //讀取單個元素,因為存入ArrayList中的元素會變為Object類型,   
  25.                          //所以,在讀取時間,   
  26.            Student stu = students[0] as Student;   
  27.                           stu.say();   
  28.   
  29.                           //遍曆元素 -- 通過索引   
  30.            for(int i = 0;i < students.Count;i ++)   
  31.                         {   
  32.                                Student a = students[i] as Student;   
  33.                                a.say();   
  34.                         }   
  35.                         //利用foreach迴圈   
  36.           foreach(Object o in students)   
  37.                        {   
  38.                                Student b = o as Student;   
  39.                                b.say();   
  40.                       }   
  41.   
  42.                       //刪除元素  通過索引刪除   
  43.          students.removeAt(0);   
  44.                       //刪除元素,    通過對象名   
  45.          students.remove(jack);   
  46.                      //清空元素   
  47.          students.Clear();   
  48.   
  49.                       //我們知道,ArrayList的容量會隨著我們的需要自動按照一定規律   
  50.          //進行填充,當我們確定不再添加元素時,我們要釋放多餘的空間   
  51.          //這就用到了Capacity屬性和TrimtoSize()方法   
  52.          //利用Capacity屬性可以查看當前集合的容量      
  53.          //利用TrimtoSize()方法可以釋放多餘的空間   
  54.             
  55.   
  56.          //查看當前容量   
  57.          int number = students.Capacity;   
  58.                     //去除多餘的容量   
  59.         students.TrimtoSize();   
  60.                         
  61.               
  62.                               
  63.   
  64.                             
  65.              }   
  66. }  

聯繫我們

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