C# 泛型集合实例应用浅析
2009-11-20 09:23
573 查看
C# 泛型集合了解之前我们明白集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一。C# 泛型是C# 2.0中的新增元素(C++中称为模板),主要用于解决一系列类似的问题。这种机制允许将类名作为参数传递给泛型类型,并生成相应的对象。将泛型(包括类、接口、方法、委托等)看作模板可能更好理解,模板中的变体部分将被作为参数传进来的类名称所代替,从而得到一个新的类型定义。泛型是一个比较大的话题,在此不作详细解析,有兴趣者可以查阅相关资料。
C# 泛型集合类用起来十分的方便快捷。在这篇随笔里面,我将用链表来模拟c#中的 List﹤T﹥ 类的行为,废话不多说,下面来看我的实现代码,代码中已经写了注释,所以不再对代码进行额外的说明:
using System.Collections;
class MyList﹤T﹥
{
private MyListNode firstNode;//首节点
private int count;//C# 泛型集合-节点计数
public MyList()
{
this.firstNode = null;
this.count = 0;
}
//C# 泛型集合-得到List长度
public int GetLength()
{
return this.count;
}
//增加一个节点
public void AddElement(T data)
{
MyListNode first = this.firstNode;
if(first==null)
{
this.firstNode=new MyListNode(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode(data);
this.count++;
}
//C# 泛型集合-删除一个节点
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = first.next;
this.count--;
return true;
}
while (first.next!=null)
{
if (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
}
return false;
}
//C# 泛型集合-得到指定索引上的集合元素
public T GetAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
; else
{
while (innercount ﹤ index)
{
first = first.next;
innercount++;
}
return first.data;
}
}
//在指定的索引上插入新的元素
public void InsertAtIndex(int index,T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
MyListNode newNode = new MyListNode(data);
newNode.next = first.next;
first.next = newNode;
}
this.count++;
}
//C# 泛型集合-删除指定索引上的集合元素
public void RemoveAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = first.next;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
first.next = first.next.next;
}
this.count--;
}
//C# 泛型集合-删除集合中的所有元素
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
//为实现该集合类能用foreach进行遍历
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first!= null)
{
yield return first.data;
first = first.next;
}
}
//内部节点类
private class MyListNode
{
public T data { get; set; }//节点上的元素值
public MyListNode next { get; set; }//节点的下一个节点
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
}
下面是C# 泛型集合对这个模拟类的使用:
class Program
{
static void Main(string[] args)
{
MyList﹤string﹥ ml = new MyList﹤string﹥();
ml.AddElement("xu");
ml.AddElement("jin");
ml.AddElement("lin");
ml.AddElement("love");
ml.AddElement("jasmine");
ml.InsertAtIndex(4, "fiercely");
ml.RemoveAtIndex(2);
ml.Remove("lin");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
C# 泛型集合实例应用的基本内容就向你介绍到这里,希望对你了解和学习C# 泛型集合有所帮助。
C# 泛型集合类用起来十分的方便快捷。在这篇随笔里面,我将用链表来模拟c#中的 List﹤T﹥ 类的行为,废话不多说,下面来看我的实现代码,代码中已经写了注释,所以不再对代码进行额外的说明:
using System.Collections;
class MyList﹤T﹥
{
private MyListNode firstNode;//首节点
private int count;//C# 泛型集合-节点计数
public MyList()
{
this.firstNode = null;
this.count = 0;
}
//C# 泛型集合-得到List长度
public int GetLength()
{
return this.count;
}
//增加一个节点
public void AddElement(T data)
{
MyListNode first = this.firstNode;
if(first==null)
{
this.firstNode=new MyListNode(data);
this.count++;
return;
}
while (first.next != null)
{
first = first.next;
}
first.next = new MyListNode(data);
this.count++;
}
//C# 泛型集合-删除一个节点
public bool Remove(T data)
{
MyListNode first = this.firstNode;
if (first.data.Equals(data))
{
this.firstNode = first.next;
this.count--;
return true;
}
while (first.next!=null)
{
if (first.next.data.Equals(data))
{
first.next = first.next.next;
this.count--;
return true;
}
}
return false;
}
//C# 泛型集合-得到指定索引上的集合元素
public T GetAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
; else
{
while (innercount ﹤ index)
{
first = first.next;
innercount++;
}
return first.data;
}
}
//在指定的索引上插入新的元素
public void InsertAtIndex(int index,T data)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = new MyListNode(data);
this.firstNode.next = first;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
MyListNode newNode = new MyListNode(data);
newNode.next = first.next;
first.next = newNode;
}
this.count++;
}
//C# 泛型集合-删除指定索引上的集合元素
public void RemoveAtIndex(int index)
{
int innercount = 1;
MyListNode first = this.firstNode;
if (index ﹥ count)
{
throw new Exception("Index out of boundary");
}
if (index == 1)
{
this.firstNode = first.next;
}
else
{
while (innercount ﹤ index - 1)
{
first = first.next;
innercount++;
}
first.next = first.next.next;
}
this.count--;
}
//C# 泛型集合-删除集合中的所有元素
public void RemoveAll()
{
this.firstNode = null;
this.count = 0;
}
//为实现该集合类能用foreach进行遍历
public IEnumerator GetEnumerator()
{
MyListNode first = this.firstNode;
while (first!= null)
{
yield return first.data;
first = first.next;
}
}
//内部节点类
private class MyListNode
{
public T data { get; set; }//节点上的元素值
public MyListNode next { get; set; }//节点的下一个节点
public MyListNode(T nodeData)
{
this.data = nodeData;
this.next = null;
}
}
}
下面是C# 泛型集合对这个模拟类的使用:
class Program
{
static void Main(string[] args)
{
MyList﹤string﹥ ml = new MyList﹤string﹥();
ml.AddElement("xu");
ml.AddElement("jin");
ml.AddElement("lin");
ml.AddElement("love");
ml.AddElement("jasmine");
ml.InsertAtIndex(4, "fiercely");
ml.RemoveAtIndex(2);
ml.Remove("lin");
foreach (string s in ml)
{
Console.WriteLine(s);
}
}
}
C# 泛型集合实例应用的基本内容就向你介绍到这里,希望对你了解和学习C# 泛型集合有所帮助。
相关文章推荐
- C# 泛型集合实例应用
- C#反射静态方法开发实例详解(从DataReader将数据填充到数据实体泛型集合)
- 使用泛型集合模拟双色球开奖C#实例
- C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
- C#中关于在List集合类中使用泛型的例子
- c# 泛型集合Dictionary
- Java 回顾笔记_集合框架-泛型高级应用
- C# 操作Excel基础实例浅析
- C#获取泛型集合中元素的类型
- 泛型应用之多对多实例
- C# 中的泛型应用
- C#读取word文档实例浅析
- c#中实例方法和静态方法的辨析与应用
- C#高级------泛型集合
- C# 反射应用实例-获取当前Color类的所有颜色
- 机房重构----C#中将DataTable类型转化为泛型集合
- java中Map集合的常用遍历方法及HashMap的应用实例
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- C# 泛型方法中反射常用方法浅析
- C#流的应用实例