C#中集合的使用--ArrayList

集合:可以使用集合来维护对象组。

  C#中的数组实现为 System.Array 类的实例,它们只是集合类(Collection Classes)中的一种类型。集合类一般用于处理对象列表,其功能比简单数组要多,功能大多是通过实现 System.Collections 名称空间中的接口而获得的,

  因此集合的语法已经标准化了。这个名称空间还包含其他一些有趣的东西,例如,以与 System.Array 不同的方式实现这些接口的类。集合的功能(包括基本功能,例如,用[index]语法访问集合中的项)可以通过接口来实现,

  该接口不仅没有限制我们使用基本集合类,例如 System.Array,相反,我们还可以创建自己的定制集合类。这些集合可以专用于要枚举的对象(即要从中建立集合的对象)。这么做的一个优点是定制的集

  合类可以是强类型化的。也就是说,从集合中提取项时,不需要把它们转换为正确的类型。

  System.Collections 命名空间中的几个接口提供了基本的组合功能: 

  IEnumerable 可以迭代集合中的项。
  ICollection(继承于 IEnumerable)可以获取集合中项的个数,并能把项复制到一个简单的数组类型中。
  IList(继承于 IEnumerable 和 ICollection)提供了集合的项列表,允许访问这些项,并提供其他一些与项列表相关的基本功能。
  IDictionary(继承于 IEnumerable 和 ICollection)类似于 IList,但提供了可通过键值(而不是索引)访问的项列表。

  System.Array 类实现 IList、 ICollection 和 IEnumerable,但不支持 IList 的一些更高级的功能,它表示大小固定的项列表。

  ArrayList基本使用方法如下:

 1         static void Main(string[] args)
 2         {
 3             //arrayList容量为10
 4             ArrayList arrayList1 = new ArrayList(10);
 5
 6            // arrayList1[0] = 1;//此时不能通过索引来访问ArrayList否则会抛出 System.ArgumentOutOfRangeException
 7             //给ArrayList添加元素
 8             arrayList1.Add(1);
 9             arrayList1.Add(2);
10             arrayList1.Add(3);
11             arrayList1.Add(4);
12             arrayList1.Add("hello");
13             arrayList1.Add("ArrayList");
14
15             arrayList1[2] = 4;//这里可以通过索引方式访问ArrayList
16
17             Console.WriteLine("arrayList1 count: {0}", arrayList1.Count);
18
19             foreach (object i in arrayList1)
20             {
21                 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
22             }
23
24             Console.WriteLine("remove item at index 4");
25             arrayList1.RemoveAt(4);
26
27             foreach (object i in arrayList1)
28             {
29                 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
30             }
31
32             Console.WriteLine("remove item 2");
33             arrayList1.Remove(2);
34
35             foreach (object i in arrayList1)
36             {
37                 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
38             }
39
40             Console.WriteLine("\n\n");
41             //通过ArrayList1创建ArrayList2
42             ArrayList arrayList2 = new ArrayList(arrayList1);
43             Console.WriteLine("arrayList2 count: {0}", arrayList1.Count);
44             foreach (object i in arrayList1)
45             {
46                 Console.WriteLine("arrayList2 item {0} type:{1}", i, i.GetType());
47             }
48
49             Console.WriteLine("\n\nPress any key to exit!");
50             Console.ReadKey();
51         }

  运行结果:

arrayList1 count: 6
arrayList1 item 1 type:System.Int32
arrayList1 item 2 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item hello type:System.String
arrayList1 item ArrayList type:System.String
remove item at index 4
arrayList1 item 1 type:System.Int32
arrayList1 item 2 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item ArrayList type:System.String
remove item 2
arrayList1 item 1 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item 4 type:System.Int32
arrayList1 item ArrayList type:System.String

arrayList2 count: 4
arrayList2 item 1 type:System.Int32
arrayList2 item 4 type:System.Int32
arrayList2 item 4 type:System.Int32
arrayList2 item ArrayList type:System.String

Press any key to exit!

  

  自定义集合

  可以通过继承System.Collections.CollectionBase 类的方式自定义集合,CollectionBase 类有接口 IEnumerable、 ICollection 和 IList,但只提供了一些简要的实现代码,特别是 IList 的 Clear()和 RemoveAt()方法,

  以及 ICollection 的 Count 属性。如果要使用提供的功能,就需要自己执行其他代码。

  为便于完成任务, CollectionBase 提供了两个受保护的属性,它们可以访问存储的对象本身。我们可以使用 List 和 InnerList, List 可以通过 IList 接口访问项, InnerList 则是用于存储项的 ArrayList对象。

 1     public abstract class Animal
 2     {
 3         private string name;
 4         public string Name
 5         {
 6             get { return name; }
 7             set { name = value; }
 8         }
 9
10         public Animal()
11         {
12             name = "NULL";
13         }
14
15         public Animal(string newName)
16         {
17             name = newName;
18         }
19
20         public void Feed()
21         {
22             Console.WriteLine("{0} has been fed.", name);
23         }
24     }
25
26     public class Dog:Animal
27     {
28         public void Run()
29         {
30             Console.WriteLine("Dog run....");
31         }
32
33         public Dog(string newName)
34             : base(newName)
35         {
36
37         }
38     }
39
40     public class Bird : Animal
41     {
42         public Bird(string newName)
43             : base(newName)
44         {
45
46         }
47
48         public void Fly()
49         {
50             Console.WriteLine("Bird fly....");
51         }
52     }
53
54     class Animals:CollectionBase
55     {
56         public void Add(Animal animal)
57         {
58             List.Add(animal);
59         }
60
61         public void Remove(Animal animal)
62         {
63             List.Remove(animal);
64         }
65
66         public Animals()
67         {
68
69         }
70
71         //通过下面代码使得Animals可以通过索引访问
72         public Animal this[int animalIndex]
73         {
74             get
75             {
76                 return (Animal)List[animalIndex];
77             }
78             set
79             {
80                 List[animalIndex] = value;
81             }
82         }
83     }
84
85         static void Main(string[] args)
86         {
87             Animals animals = new Animals();
88             animals.Add(new Dog("Jack"));
89             animals.Add(new Bird("Jason"));
90
91             foreach (Animal animal in animals)
92             {
93                 animal.Feed();
94             }
95
96             Console.WriteLine("\n\n\nPress any key to exit!");
97             Console.ReadKey();
98         }

  运行结果:

Jack has been fed.
Jason has been fed.

Press any key to exot!

  其中, Add()和 Remove()方法实现为强类型化的方法,使用 IList 接口中用于访问项的标准 Add()方法。该方法现在只用于处理 Animal 类或派生于 Animal 的类,而前面介绍的 ArrayList 实现代码可处理任何对象。

  索引符(indexer)是一种特殊类型的属性,可以把它添加到一个类中,以提供类似于数组的访问。this 关键字与方括号中的参数一起使用,但这看起来类似于其他属性。这个语法是合理的,因为在访问索引符时,将使用对象名,

  后跟放在方括号中的索引参数(例如 MyAnimals[0])。

时间: 2024-08-01 01:20:34

C#中集合的使用--ArrayList的相关文章

C#中集合ArrayList与Hashtable的使用

C#中集合ArrayList与Hashtable的使用 http://blog.csdn.net/linukey/article/details/42506819 ArrayList: 一. 注意事项: 1.可以指定ArrayList集合的初始大小 var list = new ArrayList(10);//容纳10个元素 若不指定大小,则默认大小为0,添加一个后为4,然后以倍数递增. 2.ArrayList是Array的复杂版本,ArrayList内部封装了一个Object类型的数组,从一般

C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)

C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary) int [] numbers = new int[5]; // 长度为5,元素类型为 int.string[,] names = new string[5,4]; // 5*4 的二维数组byte[][] scores = new byte[5][]; // 长度为 5 的数组,元素为 byte的数组,元素数组的长度未知. 不同的格式:int[] numbers = new int[5]

Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例

java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例 概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayList.先对ArrayLis

Java集合框架:ArrayList

ArrayList定义 package java.util; public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ private static final int DEFAULT_CAPACITY = 10; private static final Object[] EMPTY_ELEME

Java中集合与数组的切换

在Java开发中常常遇见集合与数组的互相切换,如何实现呢,呵呵呵,很简单: import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class Test { /** * 将Set集合转换为数组 * * @author GaoHuanjie */ private static void setT

JAVA中集合转数组遍历

JAVA中集合的遍历的一种方法时集合转数组遍历,也是就调用Collection中的toArray(). 代码: public static void main(String[] args) {        // TODO Auto-generated method stub        Collection c=new ArrayList();        c.add(new Student("kj",12));        c.add(new Student("uj

C#中数组Array、ArrayList、泛型List&lt;T&gt;的比较

在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序集合.数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合. Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义. 数组在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也非常简单. Array数组具体用法: using System; names

c#中的数组、ArrayList、List区别

数组 ArrayList 泛型List 总结 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. (1)数组引入的命名空间:using System; (2)Array:用法基本与数组同,引入命名空间:using System; (3)ArrayList:引入命名空间: using System.Collections (4)List:引入命名空间:using System.Collections.Generic; 数组 数组在C#中最早出现的.在内存

【转】Java如何克隆集合——深度拷贝ArrayList和HashSet

原文网址:http://blog.csdn.net/cool_sti/article/details/21658521 原英文链接:http://javarevisited.blogspot.hk/2014/03/how-to-clone-collection-in-java-deep-copy-vs-shallow.html 程序员通常会误用集合类(如List.Set.ArrayList.HashSet)所提供的拷贝构造函数或其它方法来完成集合的拷贝.值得记住的一点是,Java中集合提供的拷贝