.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# 之 集合ArrayList的更多相关文章

  1. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  2. 集合ArrayList

    /*集合ArrayList * 例如: * 1.创建:ArrayList<Egg> myList = new ArrayList<Egg>(); *      Egg类型的集合 ...

  3. 面向对象之集合ArrayList

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. 集合及特殊集合arrayList

    1,运用集合  arrayList 首先复制Colections加  : 创建arrayList ar =new arrayList(); ArrayList具体提供的功能:属性            ...

  5. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  6. 数组Array和列表集合ArrayList、LinkedList和Vector的区别

    一.ArrayList和Vector的区别 ArrayList与Vector主要从以下方面来说. 1.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同 ...

  7. 第三章泛型集合ArrayList 和Hashtable

    第三章泛型集集合 ArrayList 变量名 = new ArrayList();  //相当与一个容器 他的执行using 是  using System.Collections; 变量名.ADD( ...

  8. JAVA基础——集合——ArrayList

    ArrayList集合 ArrayList的一些方法(JDK1.8): 将指定的元素附加到列表的末尾,true:添加成功,false:添加失败: public boolean add(E e)    ...

  9. 反射方式,获取出集合ArrayList类的class文件对象

    /* * 定义集合类,泛型String * 要求向集合中添加Integer类型 * * 反射方式,获取出集合ArrayList类的class文件对象 * 通过class文件对象,调用add方法 * * ...

  10. Java中list集合ArrayList 中contains包含的使用

    Java中list集合ArrayList 中contains包含的使用 https://blog.csdn.net/qq_38556611/article/details/78774690

随机推荐

  1. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  2. python学习笔记--python编程基础

    一.一个隆重的仪式 我们在学习语言的时候,第一个写的程序肯定都是hello world.来写第一个程序吧,其实很简单,python的语法就是简单.优雅,一个print就搞定. 1 print('hel ...

  3. 我要好offer之 网络大总结

    1. TCP协议的状态机 TCP一共定义了11种状态,这些状态可以使用 netstat 命令查看 @左耳朵耗子 tcp系列教程: 上篇 下篇 2. TCP建立连接3次握手.释放连接4次握手 TCP包头 ...

  4. 在4418平台上如何配置GPIO口的状态

    硬件 ------------------------------------------------------------------------------------------------- ...

  5. uniSWF使用注意事项

    美术方面,也就是使用FLASH这里的用户,请注意以下几点, 1.把相同的图案做成元件: 2.凡是补间产生的动画物体,也要做成元件: 以上便可以节省大量的空间,因为当迩把图案做成元件的话,UNISWF导 ...

  6. Linux 之 文件压缩解压

    文件压缩解压 参考教程:[千峰教育] 命令: gzip: 作用:压缩文件,只能是单个文件,不能是多个,也不能是目录. 格式:gzip file 说明:执行命令会生成file.gz,删除原来的file ...

  7. Linux终端颜色设置

    http://blog.sina.com.cn/s/blog_65a8ab5d0101g6cf.html http://www.tuicool.com/articles/NRZfIj #PS1='${ ...

  8. Atcoder Grand Contest 024

    A 略 B 略 C 略 D(构造分形) 题意: 给出一个由n个点的组成的树,你可以加一些点形成一个更大的树.对于新树中的两个点i和j,如果以i为根的树与以j为根的树是同构的那么i和j颜色可以相同.问最 ...

  9. linux下添加定时任务。

    这周要做一个定时增量更新Elasticsearch索引的功能,以前没有做过定时更新,所以请教了下同事,使用crontab添加linux下的定时任务. 一.linux下定时执行任务的方法 在linux中 ...

  10. Java使用logback记录日志时分级别保存文件

    说明:一般情况下logback可以指定类使用什么样的级别显示输出日志,并且同一类可以指定不能级别,然后对应级别进行输出日志. 第一种配置: <?xml version="1.0&quo ...