C#动态数组ArrayList介绍

ArrayList是一种动态数组,其容量可随着我们的需要自动进行扩充.

ArrayList位于System.Collections命名空间中,所以我们在使用时,需要导入此命名空间.

下面,我们还是在Student类的基础上利用ArrayList操作,从而了解ArrayList的用法

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

编写测试类,了解ArrayList的方法


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();                       
              }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值