C#的6种常用集合类大比拼
作者:清清月儿
主页:http://blog.csdn.net/21aspnet/ 时间:2007.6.27
说明:MSDN没有说出几种集合类其间的区别,当然欲知更多细节可参考MSDN。
一.先来说说数组的不足(也可以说集合与数组的区别):
1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的
2.数组要声明元素的类型,集合类的元素类型却是object.
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!
二.下面讲述6种常用集合
1.ArrayList类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ArrayList al = new ArrayList();
al.Add(100);//单个添加
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (int number in new int[6] ...{ 9, 3, 7, 2, 4, 8 })
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
al.Add(number);//集体添加方法一//清清月儿 http://blog.csdn.net/21aspnet/
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] number2 = new int[2] ...{ 11,12 };
al.AddRange(number2);//集体添加方法二
al.Remove(3);//移除值为3的
al.RemoveAt(3);//移除第3个
ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("遍历方法一:");
foreach (int i in al)//不要强制转换
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);//遍历方法一
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("遍历方法二:");
for (int i = 0; i != al2.Count; i++)//数组是length
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int number = (int)al2[i];//一定要强制转换
Console.WriteLine(number);//遍历方法二
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/21aspnet/2007062701.gif)
2.Stack类
栈,后进先出。push方法入栈,pop方法出栈。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Stack sk = new Stack();
Stack sk2 = new Stack();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
sk.Push(i);//填充
sk2.Push(i);
}
foreach (int i in sk)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);//遍历
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sk.Pop();
Console.WriteLine("Pop");
foreach (int i in sk)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);
}
sk2.Peek();//弹出最后一项不删除//清清月儿 http://blog.csdn.net/21aspnet/
Console.WriteLine("Peek");
foreach (int i in sk2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (sk2.Count != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int i = (int)sk2.Pop();//清空
sk2.Pop();//清空
}
Console.WriteLine("清空");
foreach (int i in sk2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);
}
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/21aspnet/2007062702.gif)
3.Queue类
队列,先进先出。enqueue方法入队列,dequeue方法出队列。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Queue qu = new Queue();
Queue qu2 = new Queue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
qu.Enqueue(i);//填充
qu2.Enqueue(i);
}
foreach (int i in qu)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);//遍历
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
qu.Dequeue();
Console.WriteLine("Dequeue");
foreach (int i in qu)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Console.WriteLine(i);
}
qu2.Peek();//弹出最后一项不删除
Console.WriteLine("Peek");
foreach (int i in&n
分享到:
相关推荐
本文将深入探讨C#中六种常用的集合类,包括它们的特点、用法以及与其他数据结构如数组的区别,旨在帮助程序员更好地理解并选择合适的集合类以满足不同的编程需求。 ### 数组的局限性 在讨论集合类之前,有必要先...
CSharp的6种常用集合类,并用简单代码实现。
C#常用工具类代码集合Util第二版本(自己工作总结),包括常用工具类,扩展方法工具类,百度地图C#工具类,Echart工具类,Office工具类,Autofac工具类,Web开发常用工具类,Winform开发常用工具类,是自己工作十年...
最全的C#常用开发类,cs文件,使用起来非常方便。大约有100多个常用类,
在.NET框架中,C#是一种常用的编程语言,用于开发各种应用程序,包括Windows桌面应用、Web应用和移动应用等。在这些应用中,数据通常需要以某种形式存储和展示,这时实体类(Entity Class)和实体类集合(Entity ...
本资源集合分享了C#中最常用的一百多个类库,涵盖了多个关键领域。 1. 文件操作:C#中的System.IO命名空间提供了大量用于文件操作的类,如FileStream、StreamReader和StreamWriter,可进行文件的读写、追加、复制和...
6. **System.Linq**: 林纳斯查询(Language Integrated Query,LINQ)是C#中强大的数据查询工具,它使得在各种数据源上进行查询变得简单,如数组、集合、数据库等。`Enumerable`类提供了诸如`Select`、`Where`和`...
标题中的"C#工具类库类库 包含所有的常用工具类"暗示了这是一个集合,包含了多种实用工具类,能够极大地提升开发效率。这些工具类涵盖了从文件操作到网络通信的多个领域。 首先,FTP操作类是用于与FTP服务器进行...
在C#编程语言中,掌握常用的类...以上这些类是C#编程中非常基础且常用的,熟练掌握它们将极大地提高开发效率和代码质量。在实际项目中,还会根据需求涉及到更多特定领域的类,但以上这些已经涵盖了大部分日常开发工作。
本文档对C#的几种常见的集合(BitArray, Dictionary, Hashtable, NameValueCollection, Queue, Stack)的用法作了归纳,每种集合都附有完整的测试代码。(另外一种常见集合ArrayList收录在另外一份文档:<C# List使用...
在.NET框架中,C#语言提供了丰富的类库,使得开发者能够高效地进行各种操作。"C#常用类库(100多个)"这个资源包涵盖了众多实用的编程领域,包括文件处理、网络通信、HTTP交互、多线程、UI控件、Office文档操作、输入/...
在C#中,System.Collections和System.Collections.Generic命名空间提供了各种集合类,如ArrayList、LinkedList、Queue和Stack等。然而,这些非泛型集合存在一些局限性,比如类型转换的开销和安全性问题。 泛型集合...
C#是一种面向对象的编程语言,由微软公司开发,用于构建Windows、Web和移动应用程序。在C#中,函数库是一系列预定义的函数集合,它们提供了标准操作,以简化编程任务。本C#函数库包含了常用且实用的函数,非常适合...
C#常用类库合集,小编整理的,里面都是源码,有excel导入导出的,Json类库,条码类库,全都是精品,绝对是你想要的,大家快来下载。类库会在编译之后生成一个dll文件,然后其他的项目可以引用这个dll。这个大家都是...
6. **泛型**:泛型提供了一种方式来编写可重用的代码,可以处理多种数据类型,如泛型类、泛型方法和泛型接口。 7. **LINQ(Language Integrated Query)**:C#的查询表达式(LINQ)允许开发者以更自然的语法进行...
《C#源码大集合》是一个综合性的资源包,针对C#编程爱好者和开发者提供了丰富的学习和实践素材。这个集合涵盖了多个方面的C#编程知识,包括但不限于控件操作、文件管理、程序设置以及系统维护等多个核心领域。接下来...
C#常用的100个辅助类,很实用 很方便,比如:所有的页面跳转方式 Excel导入导出方式, 汉字转拼音等等
【C# WinForm小项目集合】是一系列专为初学者设计的编程实践项目,它们主要集中在Windows Forms(WinForm)应用程序开发上。Windows Forms是.NET Framework中的一个组件,用于构建桌面应用程序,它提供了丰富的用户...