C#入门教程之集合ArrayList用法详解

所属分类: 软件编程 / C#教程 阅读数: 122
收藏 0 赞 0 分享

本文实例讲述了C#入门教程之集合ArrayList用法。分享给大家供大家参考,具体如下:

.NET Framework提供了用于数据存储和检索的专用类,这些类统称集合。这些类提供对堆栈、队列、列表和哈希表的支持。大多数集合类实现系统的接口。下面我们主要来讲一下ArrayList。

ArrayList是命名空间Systrm.Collections下的一部分,它是使用大小可按需动态增加的数组实现IList接口。

ArrayList的容量是ArrayList可以保存的元素数。ArrayList的默认初始容量为0.随着元素添加到ArrayList中,容量会根据需要通过重新分配自动增加。使用证书索引可以访问此集合中的元素。此集合中的索引从零开始。

下面我们先来看一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 集合{
  //动物类
  public abstract class Animal
  {
    protected string name;   //动物的名字
    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    public Animal()
    {
      name="The animal with no name";
    }
    public Animal(string newName)
    {
      name=newName;
    }
    public void Feed()   //繁殖后代
    {
      Console.WriteLine("{0} has been fed.",name);
    }
  }
  //奶牛类
  public class Cow : Animal
  {
    public Cow(string newName): base(newName)
    { }
    public void Milk()   //产牛奶
    {
      Console.WriteLine("{0} has been milked.", name);
    }
  }
  //母鸡类
  public class Chicken : Animal
  {
    public Chicken(string newName):base(newName)
    { }
    public void LayEgg()  //下蛋
    {
      Console.WriteLine("{0} has laid an egg.", name);
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      //用数组的方法实现
      Console.WriteLine("Create an Array type collection of Animal object and use it:");
      Animal[] animalArray = new Animal[2];
      Cow myCow1 = new Cow("Deirdre");
      animalArray[0] = myCow1;
      animalArray[1] = new Chicken("Ken");
      foreach (Animal myAnimal in animalArray )
      {
        Console.WriteLine("New {0} object added to Array collection,Name={1}", myAnimal.ToString(), myAnimal.Name);
      }
      Console.WriteLine("Array collection contains {0} objects.",animalArray .Length );
      animalArray[0].Feed();
      ((Chicken)animalArray[1]).LayEgg();
      Console.WriteLine();
      //用集合的方法实现
      Console.WriteLine("Create an ArrayList type collection of Animal objects and use it;");
      ArrayList animalArrayList = new ArrayList();
      Cow myCow2 = new Cow("HayLey");
      animalArrayList.Add(myCow2);
      animalArrayList.Add(new Chicken("Roy"));
      foreach (Animal myAnimal in animalArrayList)
      {
        Console.WriteLine("New {0} object added to ArrayList collection,Name={1}", myAnimal.ToString(), myAnimal.Name);
      }
      Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count);
      ((Animal)animalArrayList[0]).Feed();
      ((Chicken)animalArrayList[1]).LayEgg();
      Console.WriteLine();
    }
  }
}

集合的创建

对于简单的数组来说,只有用固定的大小来初始化数组,才能使用它。

Animal[] animalArray = new Aimal[2];

而ArrayList集合不需要初始化其大小

ArrayList animalArrayList = new ArrayList();

这个类还有另外两个构造函数。第一个构造函数把现有的集合作为一个参数,把现有集合的内容复制到新实例中;而另一个构造函数通过一个参数设置集合的容量(capacity)。这个容量用一个int值指定,设置集合中可以包含的初始项数。但这并不是真实的容量,因为如果集合中的项数超过了这个值,容量就会自动增加一倍。

对于ArrayList集合,它没有现成的项,也没有null引用的项。我们使用ArrayList对象的Add()方法添加新项:

Cow myCow2 = new Cow("Hayley");
anmialArrayList.Add(myCow2);
anmialArrayList.Add(new Chicken("Roy"));

ArrayList对象支持IEnumerable接口,这个接口的唯一方法GetEnuumerator()可以迭代集合中的各项。所以可以与foreach一起使用

foreach (Animal myAnimal in animalArrayList)
{
  Console.WriteLine("New {0} object added to ArrayList collection,Name = {1}",myAnimal.Tostring(),myAnimal.Name);
}

ArrayList集合是System.Object对象的集合(通过多态性赋给Animal对象),所以必须用所有的项进行数据类型转换:

((Animal)animalArrayList[0]).Feed();
((Chichen)animalArrayList[1]).LayEgg();

我们可以使用Remove()和RemoveAt()方法删除项,这两个方法是在ArrayList类中实现的IList接口的一部分。它们分别根据项的引用或索引从集合中删除项:

animalArrayList.RemoveAt(0);
animalArrayList.Remove(myCow2);

ArrayList集合可以用AddRange()方法一次添加好几个项。这个方法接受带有ICollection接口的任何对象,包括数组:

animalArrayList.AddRange(animalArray);

AddRange()方法不是ArrayList提供的任何接口的一部分。这个方法专用于ArrayList类,

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#字符串操作技巧总结》、《C#数组操作技巧总结》、《C#中XML文件操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》及《C#面向对象程序设计入门教程

希望本文所述对大家C#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多