C#中Array与ArrayList的区别

摘要:
2008年8月19日(星期二)13:23,集合类Array和ArrayList(经常在3C#中使用)让许多初学C#的人感到困惑。在这里,我总结了Array和ArrayList对象之间的差异,并与大家分享。定义时需要指定长度;ArrayList的用法与普通集合的用法相同。定义时无需指定长度;当然,Array是一个抽象类,不能直接实例化,但从它继承的子类可以实例化。
2008年08月19日 星期二 13:23

       C#中经常用到的集合类Array与ArrayList是许多刚接触C#语言的人比较困惑的。这两个对象是比较有用的,而且是在很多地方适用的。这里我把自己对Array与ArrayList对象的区别总结一下,与大家分享。

主要区别:

一、Array的用法与数组几乎一样,可以看做是数组。在定义的时候需要指定长度;ArrayList的用法与普通集合一样,定义的时候不需要指定长度;当然,Array是抽象类,不能直接实例化,但是继承自它的子类可以实例化。

如:ArrayClass[] animalArray = new ArrayArrayClass[2]; //ArrayClass为继承自Array的类;

       ArrayList animalArrayList = new ArrayList();

二、Array对象在获得元素个数时通过数组的获得方式(Length属性):animalArray.Length;ArrayList对象在获得元素个数时通过集合的获得方式(Count属性):animalArrayList.Count;

三、为Array对象赋值时,通过下标的访问方式:animalArray[1] = new Chicken("鸡鸡一");Array对象通过集合添加的方式赋值:animalArrayList.Add(new Chicken("鸡鸡二"));

以下是针对Array与ArrayList的一个例子,其中包括一个基类Animal和两个继承类Chicken、Cow;当然主要的代码和功能在Program中。这几个类的代码分别为:

Animal类:

using System; using System.Collections.Generic; using System.Text;

namespace AnimalConsole {     public abstract class Animal     {         protected string name;

        public string Name         {             get             {                 return name;             }             set             {                 name = value;             }         }

        public Animal()         {             name = "没名字的动物!";         }

        public Animal(string newName)         {             name = newName;         }

        public void Feed()         {             Console.WriteLine("“{0}” 已经被喂养!",name);         }     } }

Chicken类:

using System; using System.Collections.Generic; using System.Text;

namespace AnimalConsole {     public class Chicken : Animal     {         public void LayEgg()         {             Console.WriteLine("“{0}” 已经下了一个蛋!",name);         }

        public Chicken(string newName) : base(newName)         {         }     } }

Cow类:

using System; using System.Collections.Generic; using System.Text;

namespace AnimalConsole {     public class Cow : Animal     {         public void Milk()         {             Console.WriteLine("“{0}” 已经被喂奶!",name);         }

        public Cow(string newName) : base(newName)         {         }     } }

Program类:

using System; using System.Collections; using System.Collections.Generic; using System.Text;

namespace AnimalConsole {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("创建一个 Array 类列表集合对象并使用它:");             Animal[] animalArray = new Animal[2];             Cow myCow1 = new Cow("牛牛一");             animalArray[0] = myCow1;             animalArray[1] = new Chicken("鸡鸡一");

            foreach (Animal myAnimal in animalArray)             {                 Console.WriteLine("动物 “{0}” 已经添加到 Array 类列表集合中,名叫 “{1}”",myAnimal.ToString(),myAnimal.Name);             }             Console.WriteLine("Array 类列表集合包含 {0} 个对象。",animalArray.Length);             animalArray[0].Feed();             ((Chicken)animalArray[1]).LayEgg();             Console.WriteLine();

            Console.WriteLine("创建一个 ArrayList 类列表集合对象并使用它:");             ArrayList animalArrayList = new ArrayList();             Cow myCow2 = new Cow("牛牛二");             animalArrayList.Add(myCow2);             animalArrayList.Add(new Chicken("鸡鸡二"));

            foreach (Animal myAnimal in animalArrayList)             {                 Console.WriteLine("动物 “{0}” 已经添加到ArrayList类列表集合中,名叫 “{1}”",myAnimal.ToString(),myAnimal.Name);             }             Console.WriteLine("ArrayList 类列表集合包含 {0} 个对象。",animalArrayList.Count);             ((Animal)animalArrayList[0]).Feed();             ((Chicken)animalArrayList[1]).LayEgg();             Console.WriteLine();

            Console.WriteLine("ArrayList 的附加操作:");             animalArrayList.RemoveAt(0);             ((Animal)animalArrayList[0]).Feed();             animalArrayList.AddRange(animalArray);             ((Chicken)animalArrayList[2]).LayEgg();             Console.WriteLine("名叫 “{0}” 的动物的索引为 {1}。",myCow1.Name,animalArrayList.IndexOf(myCow1));             myCow1.Name = "牛牛三";             Console.WriteLine("这个动物现在叫: “{0}”",((Animal)animalArrayList[1]).Name);             Console.ReadKey();         }     } } 运行结果如下图:

C#中Array与ArrayList的区别第1张

免责声明:文章转载自《C#中Array与ArrayList的区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Qt 学习之路:视图选择 (QItemSelectionModel)C#程序中从数据库取数据时需注意数据类型之间的对应,int16int32int64下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java精通并发-volatile与内存屏障的重要语义详细分析

在上一次https://www.cnblogs.com/webor2006/protected/p/12595201.html咱们已经对于volatile关键字的作用进行了一定的了解,这里回顾一下:   上一次对于第一条作用进行了详细的解读了,接下来则来解读一下剩下的两条:防止指令重排序、实现变量的可见性。而这俩其实都是通过一种手段来实现的:内存屏障(me...

ArrayList的使用方法

1、什么是ArrayListArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和IList接口 灵活的设置数组的大小 2、如何使用ArrayList最简单的例子:ArrayListList=newArrayList();for(int...

容器知识的重点总结

什么是容器 数组也是一种容器,可以存放对象或基本数据类型,数组的劣势在于不灵活,容器需要事先定义好,不能随着需求而改变而扩容。而容器则可以随时扩容来装对象,容器也称为集合。 容器的结构 单例集合 将数据一个一个的进行存储 双例集合 基于 key 和 value 的结构存储数据 Collection 接口 LIst接口:有序,可重复 ArrayList 容...

c# winform 智能模糊匹配 输入框

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Windows.Forms;usi...

Java:双括号初始化 /匿名内部类初始化法

偶然见到一种初始化方式,感到十分新奇: //新建一个列表并赋初值A、B、C ArrayList<String> list = new ArrayList<String>() {{ add("A"); add("B"); add("C"); }};...

java--集合框架

1.ArrayList和Vector的区别? 1.vector 的所有方法都是同步(Synchronized)的,是线程安全的(thread-safe)的,而ArrayList是线程不安全的,线程安全是会影响性能,ArrayList 比vector的性能好 2.当Vector或ArrayList元素超过初始大小时,vector会将容量翻倍,而arrayLi...