數組結構:
Array :在內存上是連續分配的,而且元素類型是一致的;
特點:是讀取快 可以坐標訪問 但是增刪慢,長度不能變
比如 int[] intArray=new int[20]; intArray[3]=10;
ArrayList:在內存上是連續分配的,元素沒有類型限制,任何元素都是當成object處理的,如果是值類型,會有裝箱操作
不定長度的 Add增加長度 索引賦值不會增加長度;讀取快 增刪慢;
ArrayList arrayList=new ArrayList();
arrayList.Add("001");
arrayList.Add("002");
arrayList.Remove("Eleven");
array[2]=32;
//刪除數據
List:核心也是Array 內存上是連續分配的 可以用索引訪問 不定長度 ;泛型,保證類型安全,避免裝箱拆箱;讀取快,增刪慢;
List<string> stringList=new List<string>();
stringList.Add("001");
鏈表類型
LinkedList:泛型的,鏈表 元素不連續分配,每個元素都有記錄前后節點;
不能通過坐標訪問, 找元素,只能遍歷,查找不方便;增刪比較方便;
LinkedList<int> linkedList=new LinkedList<int>();
linkedList.AddFirst(1);
linkedList.AddLast(10);
bool isContain=linkedList.Contains(123);
LinkedListNode<int> node123=linkedList.Find(123);
linkedList.AddBefore(node123,10);
linkedList.AddAfter(node123,11);
linkedList.Remove(123);
linkedList.RemoveFirst();
linkedList.RemoveLast();
Queue: 隊列,就是鏈表, 先進先出, 如任務延遲執行,A不斷地寫入任務 B不斷獲取任務去執行;
Queue<int> numbers=new Queue<int>();
numbers.Enqueue(1);
numbers.Enqueue(2);
numbers.Enqueue(3);
numbers.Dequeue();//移除
numbers.Peek();//不移除
Stack: 棧,是鏈表, 先進后出
Stack<int> numbers=new Stack<int>();
numbers.Push(1);
numbers.Push(2);
numbers.Push(3);
numbers.Push(4);
numbers.Pop();//讀取並移除
numbers.Peek();//讀取不移除
集合Set
HashSet:hash分布 元素間沒有關系 動態增加的 會自動去重 不是連續分布 不能用索引坐標訪問
統計用戶IP,IP投票;還可以做交叉並補的集合
HashSet<int> hashSet=new HashSet<int>();
hashSet.Add(1);
hashSet.Add(3);
hashSet.Remove(1);
HashSet<int> hashSet1=new HashSet<int>();
hashSet1.Add(1);hashSet1.Add(2);
hashSet1.IntersectWith(hashSet);//交集 1、2、3
hashSet1.UnionWith(hashSet);//並集1、2、3、7、10
hashSet1.ExceptWith(hashSet);//差集 10
hashSet1.SymmetricExceptWith(hashSet);//補集 7
*交叉並補只能執行一次*
SortedSet:排序的集合,可以去重加排序;也可以做交叉並補
SortSet<int> sortSet=new SortSet<int>();
sortSet.AddFirst(1);
sortSet.AddFirst(1);
sortSet.AddFirst(2);
sortSet.AddFirst(3);
sortSet.AddLast(10);
Key-Value
HashTable:key—value 體積可以動態增加 拿着key計算一個地址,然后放入key-value
object-裝箱拆箱 如果是不同的key得到相同的地址,第二個在前面地址上+1
查找的時候,如果地址對應的數據key不對,那就+1查找。。
浪費了空間 基於數組實現
查找數據 一次定位 增刪 一次定位;增刪改查 都很快
浪費空間,數據太多 重復定位 效率就下去了
HashTable table=new HashTable();
table.Add("1","0001");
table.Add("2","0004");
table["3"]="0003";
線程安全
Hashtable.Synchronized(table);//只有一個線程寫 多個線程讀
Dictionary:泛型 key-value 讀寫增刪改查都很快 有序的
Dictionary<int,string> dic=new Dictionary<int,string> ();
dic[1]="1";
SortedDictionary:泛型 key-value 讀寫增刪改查都很快 有序的
SortedDictionary<int,string> dic=new SortedDictionary<int,string> ();
dic[1]="1";
SortedList:泛型 key-value 讀寫增刪改查都很快 有序的 按照key排序
SortDictionary<int,string> dic=new SortDictionary<int,string> ();
dic[1]="1";
ILIst、IQueryable、IEnumerable、ICollection
接口是標識功能的,不同的接口拆開,為了接口隔離,盡管有重復
IList 可以下標訪問
IEnumerable 遍歷才會查詢比較 迭代器yield 任何數據集合 都實現了不同的數據結構,提供了統一的訪問接口 yield訪問模式
IQueryable 表達式目錄樹解析 延遲到遍歷的時候才會去執行