C#(074):列表:List T 与HashSet和只读集合
一、概述
List
二、声明及初始化
1、List
List mList=new List();
2、List
List mList0 = new List(new []{ "a", "b", "c" } );//数组
List mList1 = new List("a,b,c".Split(new char[] {','}) );//将字符串转成List
List mList2 = new List { "a", "b", "c" };//集合初始值设定项
三、常用属性和方法
1、添加元素
1、添加一个元素:
mList.Add("a");
2、添加一组元素
mList.AddRange(new [] { new Person("a", 20), new Person("b", 10), }
3、在Index处插入一个元素
mList.Insert(0,"d");//以前占此及此位以后的元素都往后移动
4、在Index处插入一组元素:
mList.InsertRange(0,new []{"E","f"});
2、删除元素
1、删除一个值:
mList.Remove("a");
2、删除索引处的元素:
mList.RemoveAt(0);
for (int i = mList.Count - 1; i >= 0; i--)
{ }
3、删除范围内的元素:
mList.RemoveRange(3, 2);//index,count
4、清空所有元素:
mList.Clear();
5、删除与指定条件匹配的所有元素:
mList.RemoveAll(p => p.Length > 1);//bool Predicate(T matach) 委托
注意:Remove()、Contais()、IndexOf、LastIndexOf()方法需要使用“相等”比较器。
List中的元素实现了IEquatable接口则使用其Equals()方法,否则默认使用Object.Equals(object)。
3、访问列表元素以及遍历列表:
1、用索引的形式访问
mList[0]
2、遍历
foreach (string s in mList)
{
Console.WriteLine(s);
}
for (int i = 0; i < mList.Count; i++)
{
Console.WriteLine(s);
}
注意:Count属性:实际包含的元素数;
Capacity:能够容纳的元素总数;
TrimExcess()方法可将容量调整为实际容量。
4、判断元素存在:
1、判断整个元素是否存在该List中:
if (mList.Contains("d"))
{ }
2、判断是否存在于指定条件匹配的元素:
if (mList.Exists(p => p.Length > 2))
{ }
3、判读是否List中每个元素都与指定条件匹配:
if (mList.TrueForAll(p => p.Length > 2))
{}
5、搜索:
查找索引
1、查找列表中某个项的第一个索引:
mList.IndexOf(T,[index],[count]);
2、查找某元素在列表中最后一个匹配 项的索引:
mList.LastIndexOf(T,[index],[count]);
3、查找与指定条件匹配的元素在列表中第一个匹配项的索引:
mList.FindIndex([startIndex],[count],match);
4、查找与指定条件匹配的元素在列表中最后一个匹配项的索引:
mList.FindLastIndex(2, 10, p => p.Length > 2);
查找元素:
1、查找与指定条件匹配的元素,返回第一个匹配元素:
string find= mList.Find( p => p.Length > 2);
2、查找与指定条件匹配的元素,返回最后一个匹配元素:
string find= mList.FindLast( p => p.Length > 2);
3、查找并返回与指定条件匹配的元素列表:
List finds= mList.FindAll( p => p.Length > 2);
6、排序:
委托签名:
int Comparison(T x, T y);
1、使用Comparision
mList.Sort((x, y) =>
{
int result = x[0].CompareTo(y[0]);
if (result == 0) { return x[1].CompareTo(y[1]); }
return result;
});
2、顺序翻转
mList.Reverse()//index,count
注意:Sort方法还可以利用ICompable和Icomparer接口排序。
7、转换:
1、将一定范围内的元素从List
mList.CopyTo([index],array,[arrryIndex],[count]);
2、将List
string[] arr=mList.ToArray();
3、创建源List
List(string) range= mList.GetRange(inex,count);
4、将当前List
List chars=mList.ConvertAll(p=>p[0]);
List RacerList=PList.ConvertAll(p=>new Racer(p.FirstName+" " +p.LastName));
Converter委托签名:
TOutput Converter(TInput input);
5、将List
string aa= String.Join(",",mList)
8、去掉重复项(Distinct)
需要引用using System.Linq;
1、默认比较器:
mList.Distinct().ToList();
2、自定义比较器
mList.Distinct(new MyComparer()).ToList();
public class MyComparer : System.Collections.Generic.IEqualityComparer
{
public bool Equals(string x, string y)
{
return x.ToUpper()==y.ToUpper();
}
public int GetHashCode(string obj)
{
return obj.ToUpper().GetHashCode();
}
}
9、只读集合
List的AsReadOnly()方法返回ReadOnlyCollection,所有修改方法抛出NotSupportedException异常。
System.Collections.ObjectModel.ReadOnlyCollection readOnlyList= mList.AsReadOnly();
四:HashSet
用来存储集合,基于Hash,可理解为没有Value,只有Key的Dictionary Add、IsSubsetOf、IsSupersetOf、Overlaps、UnionWith 共同点: 不同点: 链表是一串存储数据的链式数据结构,它的每个成员都有额外的两个空间来关联它的上一个成员和下一个成员。 所以,链表对于插入和删除操作效率会高于ArrayList,因为它存储了上一个成员和下一个成员的指针,进行插入和删除只需要改变当前LinkedListNode的Previous和Next的指向即可。
1、常用方法
var companyTeams = new HashSet2、HashSet和SortedSet的区别
1. 都是集,都具有集的特征,包含的元素不能有重复
1. HashSet的元素是无序的,SortedSet的元素是有序的五、链表 LinkedList
1、链表的内存表视图
2、实例:
static void Main(string[] args)
{
LinkedList