一、集合的概念:
集合提供一种灵活的处理方法。与数组不同,处理的对象组可根据程序更改的需要动态地增长和收缩。 对于某些集合,您可以为放入该集合的任何对象分配一个“键”,以便使用该键快速检索对象。
集合(Collection)类是专门用于数据存储和检索的类。这些类提供了对栈(stack)、队列(queue)、列表(list)和哈希表(hash table)的支持。大多数集合类实现了相同的接口。
集合分为泛型集合和非泛型集合:
1、使用非泛型集合的 需引入:System.Collections命名空间。
2、使用泛型集合需引入:System.Collections.Generic命名空间。
二、集合的分类:
非泛型类集合:(都是object 类型的)
ArrayList | 表示大小根据需要动态增加的对象数组。 |
---|---|
Hashtable | 表示根据键的哈希代码进行组织的键/值对的集合。 |
Queue | 表示对象的先进先出 (FIFO) 集合。 |
Stack | 表示对象的后进先出 (LIFO) 集合。 |
泛型类集合:
类 | 描述 |
---|---|
Dictionary<TKey, TValue> | 表示根据键进行组织的键/值对的集合。 |
List< T > | 表示可通过索引访问的对象的列表。 提供用于对列表进行搜索、排序和修改的方法。 |
Queue< T > | 表示对象的先进先出 (FIFO) 集合。 |
SortedList<TKye,TValue> | $表示根据键进行排序的键/值对的集合,而键基于的是相关的 IComparer 实现。 |
Stack< T > | 表示对象的后进先出 (LIFO) 集合。 |
三、集合成员介绍:
索引器 :
可以直接使用对象下标给数组中的元素赋值, 可以直接用下标来赋值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson集合_
{
class People
{
public int[] a;
public People()
{
a = new int[10];
}
public int this [int index]
{
get
{
return a[index];
}
set
{
a[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
People _p = new People();
_p[0] = 10;
_p[1] = 20;
Console.WriteLine(_p.a[0]);
Console.WriteLine(_p.a[1]);
}
}
}
一、栈(stack)和队列(Queue)
栈(stack): 栈和队列非常相似,只不过队列是先进先出,而栈中的数据添加和移除都在一端进行,遵守栈中的数据则后进先出。Stack类实现了ICollection和IEnumerable接口。
stack 集合有泛型和非泛型的集合:
// 非泛型集合里边存的数据都是object类型
Stack _s = new Stack();
// 泛型集合里边只能存放在指定类型的数据 隐式转换除外
Stack<int> _s1 = new Stack<int>();
// push 添加
_s1.Push(111);
_s1.Push(2222);
_s1.Push(33333);
_s1.Push(444444);
//使用foreach 遍历里边的元素,先进后出
foreach (var item in _s1)
{
Console.WriteLine("进栈" + item);
}
//找到顶部的数据并且删除掉,经常用在悔棋
int _number = _s1.Pop();
// Console.WriteLine("\n" + _number);
int _number1 = _s1.Pop();
// Console.WriteLine("\n" + _number1);
//得到顶部的数据但是不删除掉
int a = _s1.Peek();
// Console.WriteLine(a);
// 查看里边是否包含这个数字,如果包含就返回true,没有就返回false
bool _b = _s1.Contains(111);
Console.WriteLine(_b);
//把集合转化为数组
int[] _arry = _s1.ToArray();
// Console.WriteLine(_arry[0]);
队列(Queue) : 一种数据结构,数据有列表的一端插入,并由列表的另一端移除。就像单行道,只能从一段进,从一端出。
//队列 先进先出
Queue _q = new Queue();
Queue<int> _q1 = new Queue<int>();
_q1.Enqueue(1111);
_q1.Enqueue(22222);
_q1.Enqueue(333333);
//遍历的是int类型 而不是数组
foreach (int item in _q1)
{
Console.WriteLine(item);
}
//移除并返回位于 Queue 开始处的对象。
//int _n = _q1.Dequeue();
//Console.WriteLine(_n);
//返回位于 Queue 开始处的对象但不将其移除。
//int _n1 = _q1.Peek();
//Console.WriteLine(_n1);
//bool _w = _q1.Contains(1111);
//Console.WriteLine(_w);
//获取 Queue 中包含的元素数。
//int count = _q1.Count();
//Console.WriteLine(count);
//_q1.Clear();
//foreach (var item in _q1)
//{
// Console.WriteLine(item);
//}
二、Dictionary和Hashtable
Dictionary
Dictionary<TKey, TValue> 类((字典只有泛型的)): 称为字典类,表示键和值的集合。其中TKey表示字典中的类型,Tvalue表示字典中的值类型。Dictionary类实现了ICollection、IEnumerable、IDictionary·接口。
1、在字典里边,每一个元素都是一个键值对
2、故其对应的是键值对类型,KeyValuepaire,并且是泛型的
3、泛型所指定的类型 和定义字典所指定的具体类型一致
//字典 Dictionary 只有泛型的
Dictionary<string, int> _dic = new Dictionary<string, int>();
_dic.Add("张三",1);
_dic.Add("李四",2);
_dic.Add("王五",3);
//_dic["王五"] = 5;
//通过键来查找
Console.WriteLine(_dic["王五"]);
//判断是否包含这个键
bool _isB = _dic.ContainsKey("王");
Console.WriteLine(_isB);
//得到所有的键,并把键的集合都返还回来
Dictionary<string, int>.KeyCollection key = _dic.Keys;
foreach (var item in key)
{
Console.WriteLine(item);
}
//得到所有的值并把值返还回来
Dictionary<string, int>.ValueCollection value = _dic.Values;
foreach (var item in value)
{
Console.WriteLine(item);
}
//TryGetValue存在这个键 返回true 并且返回这个值
int _value;
bool _res = _dic.TryGetValue("王五",out _value);
Console.WriteLine("_value "+":" + _value);
Hashtable :
非泛型 正好是和字典互补的
Hashtable _h = new Hashtable();
_h.Add("145",125);
foreach (DictionaryEntry item in _h)
{
Console.WriteLine(item.Key + ":"+item.Value);
}
三、ArrayList和List
ArrayList:
- 该类型的集合只有非泛型的,其元素类型均为object
- 它具有数组的特性并且可以动态的添加和减少语言
//非泛型
ArrayList _array = new ArrayList();
// 可以动态的添加和减少元素
_array.Add(123);
_array.Add("m");
_array.Add(10.5f);
_array.Add(null);
_array.Add(false);
_array.Add('a');
// Console.WriteLine(_array[0] +"\n");
//有这个元素就删除,没有就不删,删除指定的
//_array.Remove('a');
//删除指定元素的下标
_array.RemoveAt(2);
foreach (var item in _array)
{
//Console.WriteLine(item + " \n");
}
//倒着排序
_array.Reverse();
foreach (var item in _array)
{
// Console.WriteLine(item);
}
List < T > (泛型, 和ArrayList是互补的)
List< T >类表示可通过索引访问的对象的强类型列表。 提供用于对列表进行搜索、排序和操作的方法。List< T >是ArrayList类的泛型等效类,该类使用大小可按需动态增加的数组实现IList< T >泛型接口。List< T >类同样也是实现了ICollection、IEnumerable和IList接口。
class Student
{
int age;
public int Age
{
get
{
return age;
}
}
public Student(int age)
{
this.age = age;
}
}
class Program
{
static void Main(string[] args)
{
List<int> _list = new List<int>();// 可以指定 元素的容量 ,默认为4个
_list.Add(12);
_list.Add(34);
_list.Add(56);
_list.Add(78);
_list.Sort();
//元素个数 和 容量
//Console.WriteLine(_list.Count);
//Console.WriteLine(_list.Capacity);
for (int i = 0; i < _list.Count; i++)
{
Console.WriteLine("{0,4}" , _list[i]);
}
List<Student> _student = new List<Student>();
Student _s = new Student(10);
Student _s1 = new Student(20);
Student _s2 = new Student(30);
_student.Add(_s);
_student.Add(_s1);
_student.Add(_s2);
foreach (var item in _student)
{
Console.WriteLine(item.Age + "");
}
// Console.WriteLine();
}
}
投票系统
Dictionary<string, int> _count = new Dictionary<string, int>();
for (int i = 0; i < 20; i++)
{
Console.WriteLine("第{0}次输入名字",i+1);
string _name = Console.ReadLine();
if (_count.ContainsKey(_name))
{
_count[_name]++;
}else
{
_count[_name] = 1;
}
}
foreach (KeyValuePair<string,int> item in _count)
{
Console.WriteLine("姓名{0}:\t 获得{1}票",item.Key,item.Value);
}