乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#動(dòng)態(tài)數(shù)組ArrayList介紹

       方衛(wèi)福 2011-03-28

      ArrayList是一種動(dòng)態(tài)數(shù)組,其容量可隨著我們的需要自動(dòng)進(jìn)行擴(kuò)充.

      ArrayList位于System.Collections命名空間中,所以我們?cè)谑褂脮r(shí),需要導(dǎo)入此命名空間.

      下面,我們還是在Student類(lèi)的基礎(chǔ)上利用ArrayList操作,從而了解ArrayList的用法

      01 public class Student   
      02 {   
      03               public Student(){}   
      04     
      05               public Student(String name,int age,String hobby)   
      06               {   
      07                            this.Name = name;   
      08                            this.Age = age;   
      09                            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 }

       

      編寫(xiě)測(cè)試類(lèi),了解ArrayList的方法


      01 using System.Collections;   
      02     
      03 public class TestStudent   
      04 {   
      05               public static void main(String args [])   
      06              {   
      07                          //建立ArrayList對(duì)象   
      08            ArrayList students = new ArrayList();   
      09     
      10                          //實(shí)例化幾個(gè)Student類(lèi)對(duì)象   
      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類(lèi)的add()方法添加元素   
      16            students.add(rose);   
      17                           students.add(jack);   
      18                           students.add(mimi);   
      19     
      20                           //利用ArrayList的Count屬性查看該集合中的元素?cái)?shù)量   
      21            int number = students.Count;   
      22                           Console.WriteLine("共有元素" + number + "個(gè)");   
      23     
      24                           //讀取單個(gè)元素,因?yàn)榇嫒階rrayList中的元素會(huì)變?yōu)镺bject類(lèi)型,   
      25                          //所以,在讀取時(shí)間,   
      26            Student stu = students[0] as Student;   
      27                           stu.say();   
      28     
      29                           //遍歷元素 -- 通過(guò)索引   
      30            for(int i = 0;i < students.Count;i ++)   
      31                         {   
      32                                Student a = students[i] as Student;   
      33                                a.say();   
      34                         }   
      35                         //利用foreach循環(huán)   
      36           foreach(Object o in students)   
      37                        {   
      38                                Student b = o as Student;   
      39                                b.say();   
      40                       }   
      41     
      42                       //刪除元素  通過(guò)索引刪除   
      43          students.removeAt(0);   
      44                       //刪除元素,    通過(guò)對(duì)象名   
      45          students.remove(jack);   
      46                      //清空元素   
      47          students.Clear();   
      48     
      49                       //我們知道,ArrayList的容量會(huì)隨著我們的需要自動(dòng)按照一定規(guī)律   
      50          //進(jìn)行填充,當(dāng)我們確定不再添加元素時(shí),我們要釋放多余的空間   
      51          //這就用到了Capacity屬性和TrimtoSize()方法   
      52          //利用Capacity屬性可以查看當(dāng)前集合的容量      
      53          //利用TrimtoSize()方法可以釋放多余的空間   
      54               
      55     
      56          //查看當(dāng)前容量   
      57          int number = students.Capacity;   
      58                     //去除多余的容量   
      59         students.TrimtoSize();                        
      60              }   
      61 }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多