C#专题——自定义集合

    技术2022-07-10  117

    一、List

    List<int> numsList = new List<int>() { 10, 20, 30 }; int count = numsList.Count;//获取集合中元素个数 int[] numsArray = new int[10]; numsList.CopyTo(1, numsArray, 1, 2);//将集合中的元素按照指定的方式复制到数组中 numsList.Add(100);//为集合添加元素 numsList.Remove(100);//删除集合中含有某一个元素的项 numsList.RemoveAt(1);//按照索引删除集合中的项 bool isExist = numsList.Contains(30);//判断集合中是否含有某个元素 int num1 = numsList.IndexOf(1);//按照索引从小到大的顺序返回指定索引的元素 int num2 = numsList.LastIndexOf(1);//按照索引从大到小的顺序返回指定索引的元素 List<int> numsList2 = new List<int>() { 10, 20, 30, 40, 50 }; int index = numsList2.BinarySearch(25);//查找被查找元素的索引,此种方法速度比Indexof快,如果没有找到元素,则返回负值,将该值按位取反后,得到的是大于被查找元素的下一个元素的索引,然后可以选择插入该元素,则将该元素插入列表中 numsList2.Insert(~index, 25); List<int> numsArray2 = numsList2.FindAll(new Predicate<int>(a => a > 30));//查找多个数据项,和下面的查询表达式的效果相同 List<int> numsArray3 = (from num in numsList2 where num > 30 select num).ToList(); int numFirst = numsList2.Find(new Predicate<int>(num => num > 39));//查找满足条件的第一个元素

    二、字典集合Dictionary<TKey,TValue>

    Dictionary<int, string> keyValuePairs = new Dictionary<int, string>() { [1] = "小", [2] = "中", [3] = "大" };c#6.0开始的声明 Dictionary<int, string> keyValuePairs2 = new Dictionary<int, string>() { { 1, "小" },{ 2,"中"},{ 3,"大"} };//c#6.0之前的声明 keyValuePairs.Add(4,"小");//添加元素 keyValuePairs.Remove(1);移除元素,根据键来移除元素 string keyValue = keyValuePairs[2];//根据键来获取元素 string keyValue1 = keyValuePairs.ContainsKey(5) ? keyValuePairs[1] : default;//最好在获取元素之前判断键是否存在,当键不存在时,会报异常 //string keyValueError = keyValuePairs[5];//键不存在时,会报异常 foreach (KeyValuePair <int ,string> item in keyValuePairs )//访问键值对集合中的元素,键和值 { Console.WriteLine(item .Key ); Console.WriteLine(item .Value ); }

    三、栈Stack(先进后出)

    Stack<int> stack = new Stack<int>(); stack.Push(1);//在栈的顶部插入元素 stack.Push(2); stack.Push(3); int stackValue = stack.Peek();//获取栈顶部元素,不删除该元素,导致后插入的元素被获取 foreach (int item in stack) { Console.WriteLine(item); }

    输出

    3 2 1 int stackValue2 = stack.Pop();//获取栈顶部元素,删除该元素 foreach (int item in stack) { Console.WriteLine(item); }

    输出

    2 1 int stackValue3 = stack.Contains(4) ? stack.Peek() : default;//判断元素是否存在

    四、队列

    Queue <int> queue = new Queue<int>(); queue.Enqueue (1);//在队列的底部插入元素 queue.Enqueue(2); queue.Enqueue(3); int queueValue = queue.Peek();//获取顶部元素,导致先插入的元素被获取 foreach (int item in queue) { Console.WriteLine(item); }

    输出

    1 2 3 int queueValue2 = queue.Dequeue (); foreach (int item in queue) { Console.WriteLine(item); }

    输出

    2 3 int queueValue3 = queue.Contains(4) ? queue.Peek () : default;

    五、链表 六、索引器 public class Name {

    public Name(T firstName, T lastName) { this.FirstName = firstName; this.LastName = LastName; } public T FirstName { get; set; } public T LastName { get; set; } public T this[NameOrder index] { get { switch (index) { case NameOrder.First: return FirstName; case NameOrder.Second: return LastName; default: return default; } } } } Name<string> name = new Name<string>("四", "李"); string firstName = name[NameOrder.First];

    七、迭代器 class IterationSample : IEnumerable { Object[] values; Int32 startingPoint; public IterationSample(Object[] values, Int32 startingPoint) { this.values = values; this.startingPoint = startingPoint; } public IEnumerator GetEnumerator() { for (int index = 0; index < this.values.Length; index++) { yield return values[(index + startingPoint) % values.Length]; } } }

    object[] values = { "a", "b", "c", "d", "e" }; IterationSample collection = new IterationSample(values, 3); foreach (object x in collection) { Console.WriteLine(x); }
    Processed: 0.011, SQL: 9