C#泛型集合类
来源:互联网 发布:情书淘宝店网址 编辑:程序博客网 时间:2024/04/28 01:31
C#提供了许多常用的集合类,比较常用的有以下几个:
List:可以像数组一样按索引访问的列表,也提供其他的方法进行搜索和排序。
创建List时无需指定大小,它随着元素的添加,可以自动增加,保证能够容下添加的元素。
———————————————————————————————————————-
方法示例:
using System;
using System.Collections.Generic;//引进泛型空间
List IntList = new List();//创建一个包含整型数据元素的 列表 IntList
int [] number = {9,8,7,6,5,4,3,2,1};
foreach(int num in number)
{
IntList.Add(num);//利用Add()方法进行列表填充
}
IntList.Insert(IntList.Count,100);//在列表的尾部添加一个元素 100
IntList.Remove(5);//移除元素 5,在索引 4 处
IntList.RemoveAt(5);//移除 索引位置是 5的元素
foreach(int num in IntList)
{
Console.WriteLine(num);//foreach 输出 列表的元素。
}
——————————————————————————————————————
Queue:实现了队列的功能,先入先出,在一端插入,另一端删除的功能。
尾部插入用Enqueue,队头移除用Dequeue
方法示例:
int[] ArrayList = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; Queue<int> IntQueue = new Queue<int>();//构建一个队列实例 foreach (int num in ArrayList ) { IntQueue.Enqueue(num);//利用Enqueue()方法填充新建队列 } int element; element = IntQueue.Dequeue();//输出队列的第一个元素 10(先入先出) Console.WriteLine(element); while(IntQueue .Count >0) { IntQueue.Dequeue();//利用Dequeue()方法清空队列 } IntQueue.Enqueue(100);//空队列添加一个元素 element = IntQueue.Dequeue();//输出刚添加的元素 100 Console.WriteLine(element); Console.ReadKey();———————————————————————————————————————————Stack<T>:实现了栈结构,后人先出,只在栈顶进行操作,插入和弹出都在栈顶操作。插入操作用push(),移除操作用Pop();方法示例:
int[] ArrayList = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };Stack<int> IntStack = new Stack<int>();//构建一个整数栈数列 foreach (int num in ArrayList ) { IntStack.Push(num);//push()方法填充栈 } int element; element = IntStack.Pop();//Pop()弹出栈顶元素 Console.WriteLine(element); foreach (int num in IntStack )//遍历栈 { Console.WriteLine(num); } while (IntStack .Count >0)//遍历栈 { int ele = IntStack.Pop(); Console.WriteLine(ele); }
“`
————————————————————————————————————————————
- C#集合类:Dictionary泛型集合
- C# 集合类:Dictionary 泛型集合
- C#泛型集合类
- C#集合 泛型集合
- C#泛型集合
- C#泛型集合
- C#泛型集合
- c#泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类之 Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- C# 集合类(六):Dictionary 泛型集合
- Linux 文件与目录管理
- 2017-05-28 DBA日记,oracle生僻字处理
- POJ-3624-01背包
- Python数据分析与展示 | 【第一周】数据分析之表示 课后作业
- java基础篇(十一)——详细解析Java中抽象类和接口的区别
- C#泛型集合类
- web.xml中 <load-on-startup>笔记
- Linux 磁盘管理
- C语言实现大数的减法
- 排序算法——快速排序(Quick Sort)
- android开发-获取wifi列表
- NS2初学笔记(三)之 最详细讲解ns2运行机制
- Linux vi/vim
- 莫比乌斯反演总结