集合

Array

Array是个抽象类,不能实例化。Array的具体实现

Person[] people = new Person[] { new Person("nihao",1),new Person("zhishu", 3)};

Array一旦确定就不能改变长度。

List

1. List 与Array的不同

List vs Array: Final Thoughts
Let’s conclude the list vs array. Inserting parts in the middle of the list is exhausting since arrays are contiguous in memory. Lists allow straightforward insertion into lists. An array is a method of organizing data in a memory device. A list is a data structure that supports several operations. An array is a collection of homogenous parts, while a list consists of heterogeneous elements. Array memory is static and continuous. List memory is dynamic and random. Users don’t need to confine track of next memory with arrays. With lists, a user has to track of next location.
from list vs array

contiguous adj 连续不断的
homogenous adj 同类型的
heterogeneous adj 各种各样的

从内存上来所list是面向对象的。array虽然是引用类型,但存储方式却在内存上连续。
因此array不宜存大结构,且不能从中插入。适合存比较小的、临时使用的集合。头部应该存的是首地址
list实际上就不是数组了,头部存的是个索引表(就是地址数组),内部数据零散储存。

2.List构造器

List的property有:Capacity,Count和一个索引器
Capacity是容量,是给List申请内存时的大小。Count是在List内的数值。Capacity永远比Count大。当出现Cpacity不够时会Resize,resize后的Capacity永远是2的指数(就是会扩大一位数)。

List<int> li = new List<int>();//

List<int> list = new List<int>(2);//
for (int i = 0; i < 9; i++)
    list.Add(1);
foreach (var i in list)
    Console.WriteLine(i);
Console.WriteLine("capacity="+list.Capacity+" count="+list.Count);

List<int> _list = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 });//
Console.WriteLine("capacity=" + _list.Capacity + " count=" + _list.Count);

CRUD操作:

List<string> _list = new List<string>(new string[] { "1","2","3","4","5" });//
Console.WriteLine("capacity=" + _list.Capacity + " count=" + _list.Count);

_list.Add("added");
_list.Insert(3,"inserted");//头插,所有操作都是看Index
_list.RemoveAt(5);
foreach (var i in _list)
            Console.Write(i + " ");
			
//1 2 3 inserted 4 added

排序:
算法是快速排序

Sort(Comparison<T>) 委托,返回值为int,参数为T x,T y
Sort(Int32, Int32, IComparer<T>) 范围排序
Sort() Sorts the elements in the entire List using the default comparer.
Sort(IComparer<T>) Sorts the elements in the entire List using the specified comparer.
_list.Sort();
foreach (var i in _list)
    Console.Write(i + " ");
	
//1 2 3 4 added inserted

查找:

  • 查内容
    Find(T)
    FindLast(T)
  • 查序号
    IndexOf()
    LastIndexOf()
  • 根据委托内容查找
    FindIndex(Predicate<T>)委托返回值bool 单个参数T
    FindLastIndex(Predicate<T>)

ArrayList

和List类似,推荐用List。

Stack and Queue

和常见的数据结构用法相同

HashSet和Dictionary

  • HashSet:

A HashSet, similar to a Dictionary, is a hash-based collection, so look ups are very fast with O(1). But unlike a dictionary, it doesn’t store key/value pairs; it only stores values. So, every objects should be unique and this is determined by the value returned from the GetHashCode method. So, if you’re going to store custom types in a set, you need to override GetHashCode and Equals methods in your type.
from Collections in C#

内容不能重复,只存值,没有键值对。如果内部储存自定的类,要重写GetHashCodeEquals

  • Dictionary:键值对

      Dictionary<int, string> dic = new Dictionary<int, string>(new List<KeyValuePair<int, string>> {
              new KeyValuePair<int,string>(1,"nihao"),new KeyValuePair<int,string>(2,"shiwo"),
              new KeyValuePair<int,string>(3,"caishi")
              });
          dic.Add(4, "sssss");
    
posted @ 2021-08-17 09:24  none323  阅读(33)  评论(0)    收藏  举报