第十章集合与泛型
目标
使用System.Array操作数组
集合
- 使用System.ArrayListi对象
- 使用System.Hashtable对象
理解泛型集合类
- List
对象 - 掌握Dictionary<TKey,TValue>
掌握Lambda表达式
IComparable接口的应用
泛型接口
- IComparable
接☐ - IComparer
接口
System.Array
- 抽象的基类
- 提供Createlnstance方法创建数组
- 可以是多维的
- 下标可设置
System.Array的属性和方法
属性 | 描述 |
---|---|
Length | 得到数组所有维元素总个数的属性 |
实例方法 | 描述 |
---|---|
Copyto() | 将一个一维数组中的所有元素复制到另一个一维数组中 |
GetLength() | 返回指定维的元素个数 |
GetVaule | 通过索引返回指定元素的值 |
SetVaule | 将数组中的指定元素设为指定值 |
静态方法 | 描述 |
---|---|
BinarySearch() | 使用二进制搜索方法搜索一维已排序数组中的某个值 |
Clear() | 将数组中一组元素设为0或null |
Copy() | 将数组中的一部分元素复制到另一个数组中 |
CreateInstance() | 初始化Array类的实例 |
Indexof() | 返回给定值在一维数组中首次出现的位置索引 |
Last Indexof() | 返回给定值在一维数组中最后一次出现的位置索引 |
Reverse() | 反转给定一维数组中元素的顺序 |
Resize() | 将数组的大小更改为指定的新大小 |
Sort() | 将数组中的元素进行排序,只能对一维数组从小到大排序 |
实例
static void Main(string[] args)
{
//系统类可以按F12看定义
//用Array(抽象类)创建数组
Array arr = Array.CreateInstance(typeof(int),5);//创建int类型长度为5的数组
arr.SetValue(89, 0);//为数组元素设置值
arr.SetValue(95, 1);
Console.WriteLine(arr.Length);
foreach (int item in arr)
{
Console.WriteLine(item);
}
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("数组中第{0}个元素是:{1}",i+1,arr.GetValue(i));
}
}
集合
- System.Collectionsi命名空间
- ArrayList类
- Hashtable类
集合-ArrayList类
容量
- 可以根据需要动态扩展。通过设置ArrayList..Capacityl的值可以执行重新分配内存和复制元素等操作
批量操作
- 提供的方法在某个时间追加、插入或移出一组元素
- 一维
- 下标不可设置
实例
static void Main(string[] args)
{
//实例化一个集合
ArrayList list = new ArrayList(4);
list.Add(10);
list.Add("FSY");
list.Add(101.101);
People pl = new People();
list.Add(pl);
foreach (var item in list)
{
Console.WriteLine(item);
}
list.Add("1");
//移除集合中的元素,集合容量不变
list.Remove(10);
//如果添加元素时添加的个数大于容量时,容量会自动增加一倍
Console.WriteLine("集合的个数是"+list.Count);
Console.WriteLine("集合的容量是"+list.Capacity);
//将集合容量设置为集合的实际个数
list.TrimToSize();
Console.WriteLine("集合的个数是" + list.Count);
Console.WriteLine("集合的容量是" + list.Capacity);
}
集合-Hashtable类
- 将数据作为一组键值对来存储
键
- 索引器
- 不可重复
属性 | 描述 |
---|---|
Count | 该属性用于获取哈希表中键值对的数量 |
方法 | 描述 |
---|---|
Add() | 将一个键值对添加到哈希表中 |
ContainKey() | 测试键是否已经存在 |
Remove() | 根据键将对应的键值对从哈希表移除 |
实例
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1,"FSY");
ht.Add(2, "ZLY");
ht.Add("FSY", "ZLY");
Console.WriteLine("有{0}个元素",ht.Count);
Console.WriteLine(ht.ContainsKey("FSY"));
if (ht.ContainsKey(1)==true)
{
Console.WriteLine(ht[1]);
}
//集合没有类型限制,很容易出现类型访问错误,类型是不安全的
foreach (var item in ht.Keys)
{
Console.WriteLine(ht[item]);
}
}
泛型集合
- 明确指定放入集合中对象的数据类型
优点
类型安全
- 使用泛型集合编译器会在编译期间检查要放入集合的对象的数据类型,如果发现不是某种特定的类型就会报错
提高性能
- 存取数据时不会发生类型转换,特别是存取值类型时不会发生装箱和拆箱操作
System.Collections.Generici命名空间
List<T>
Dictionary<K,V>
List
语法
泛型集合类<数据类型>实例名=new泛型集合类<数据类型>()
- 添加和访问元素、插入和删除元素、清空集合、把元素复制到数组中,而且可以搜索和转换元素、使元素逆序等高级操作
- FindAll()方法检索
- ForEach()方法可以使用委托对集合的每一个成员进行操作
实例
class Program
{
static void Main(string[] args)
{
//泛型集合
//定义一个Student类型的泛型集合
List<Student> list = new List<Student>();
//实例化4个学生对象
Student stu1 = new Student("张三", 1);
Student stu2 = new Student("李四", 2);
Student stu3 = new Student("王五", 3);
Student stu4 = new Student("赵六", 4);
//向泛型集合添加元素
list.Add(stu1);
list.Add(stu2);
list.Add(stu3);
list.Add(stu4);
Console.WriteLine(list.Count);
foreach (Student item in list)
{
Console.WriteLine("学号:{0},姓名:{1}",item.StuId,item.Name);
}
//效果与foreach一致
list.ForEach(delegate (Student s)
{
Console.WriteLine("学号:{0},姓名:{1}", s.StuId,s.Name);
});
//在泛型集合中查找所有学号小于3的学生
List<Student> listFind = list.FindAll(delegate (Student s)
{
if (s.StuId < 3)
{
return true;
}
else
{
return false;
}
});
listFind.ForEach(delegate (Student s)
{
Console.WriteLine("学号:{0},姓名:{1}", s.StuId, s.Name);
});
}
Dictionary<K,V>
语法
Dictionary<数据类型,数据类型>实例名=new Dictionary<数据类型,数据类型>();
- Dictionary<TKey,Value>类的功能与HashTable类类似,也是通过键值对来存储元素。
- TKey表示键的数据类型
- TValue表示值的数据类型。
实例
class Program
{
static void Main(string[] args)
{
//泛型集合
//定义一个Student类型的泛型集合
List<Student> list = new List<Student>();
//实例化4个学生对象
Student stu1 = new Student("张三", 1);
Student stu2 = new Student("李四", 2);
Student stu3 = new Student("王五", 3);
Student stu4 = new Student("赵六", 4);
//向字典类添加元素
dicStu.Add(stu1.StuId, stu1);
dicStu.Add(stu2.StuId, stu2);
dicStu.Add(stu3.StuId, stu3);
dicStu.Add(stu4.StuId, stu4);
//dicStu.Add(stu4.Name,stu4);错,数据类型必须保持一致
foreach (int item in dicStu.Keys)
{
Console.WriteLine(dicStu[item].Name);
}
}
对象与集合初始化器
- 集合初始化器用来初始化一个集合,它由一系列元素组成,并封闭于“{”和“}”标记内
//对象初始化器
Student s1 = new Student{SId=101, SName="张三"};
Student s2 = new Student{SId=102, SName="李四"};
//集合初始化器
List<Student> lstStu = new List<Student> {s1,s2};
实例
static void Main(string[] args)
{
//实例化学生对象
//对象初始化器
Student stu1 = new Student { SId = 1, Name = "FSY" };
Student stu2 = new Student { SId = 2, Name = "YSF" };
//集合初始化器
List<Student> list = new List<Student> { stu1, stu2 };
foreach (Student item in list)
{
Console.WriteLine("学号:"+item.SId+",姓名"+item.Name);
}
}
Lambda表达式
- s是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或飙到式目录树类型
- 基本形式
(inputparameter)=>expression
- =>读作“goes to”
- Lambda语句
(inputparameter)=>{statement;};
实例
static void Main(string[] args)
{
//Lambda表达式
//语法:参数=>表达式
int[] arr = { 436, 67, 78, 9, 3, 57, 2, 7, 9, 1 };
//查找数组中奇数的个数
Console.WriteLine(arr.Count(n => n % 2 == 1));
/*
public static bool JiSuan(int n)
{
if (n %== 1)
{
return truwe;
}
else
{
return false;
}
}
*/
}
总结
Array类
- Createlnstance方法来创建Array对象
- 属于System命名空间
- 集合用于管理在运行时动态创建的元素项
集合
- ArrayList类可以批量操作
- Hashtable类键值对
- System.Collections命名空间
泛型集合
- List
- Dictionary<Tkey,Tvalue>
- 类型安全
- 属于System.Collections.Generic命名空间
- List
Lambda表达式
- 一种更纯粹的匿名方法
评论 (0)