C 中集合的使用

2021-08-08 11:34:01 字數 1531 閱讀 8375

c#中的集合介面:

ienumerable

ienumerable>

icollection

icollection>

idictionary

idictionary>

ilist

ilist>

集合介面的關係

inte***ce ienumerablet>

: ienumerable

inte***ce icollection : ienumerable

inte***ce 

icollection>

: ienumerable>

, ienumerable

inte***ce idictionary : icollection, ienumerable

inte***ce idictionary:

icollection>, ienumerable>, ienumerable

inte***ce ilist : icollection, ienumerable

inte***ce ilist>

: icollection>

, ienumerable>

, ienumerable

泛型被引用進來後,之前的集合類就可以使用泛型類來替換,我們來看看對應的替換關係

arraylist集合類---->list>

hashtable集合類--->dictionary

queue--->queue>

stack--->stack>

泛型類引進來的好處是它是型別安全的,泛型集合類分兩類,一類是鍵值對集合,另一類是不通過key操作的集合

鍵值對集合

dictionary

優點:查詢插入速度快

缺點:資料無序,如果要按指定順序來遍歷,則缺點明顯

sorteddictionary

按照key進行了排序的集合,查詢遍歷方便

sortedlist

資料實際儲存在陣列中,所以新增或刪除元素時,要移動的元素可能很多,但是查詢較快,所以對於不需要要過來的新增或刪除,而且按一定的順序遍歷的集合,可以使用sortedlist.

非鍵值對集合類

list>

內部實際是乙個陣列,預設長度為4,當元素超過4個後,會以2倍的長度進行擴充套件,並建立新陣列,把之前的陣列元素拷貝到新陣列中,所以如果知道長度,最好提前定義長度,以免頻繁的建立拷貝陣列,可以使用下標訪問

linkedlist>

內部維護的是乙個雙向鍊錶結構,所以對於新增或刪除元素操作比較方便,如果運算元據頻繁,可以使用這個集合類

hashset>

有資料唯一性特徵的無序集合類,不支援下標訪問,使用foreach進行遍歷

sortedset>

按資料型別進行排序的資料唯一的集合類

stack>

棧集合,後進先出,不支援下標訪問

queue>

佇列集合,先進先出,不支援下標訪問

Oracle中集合的使用

兩個表通常使用外來鍵建立資料之間的關聯,相對於這樣的方式訪問資料庫,儲存在集合中的資料可以更快的被訪問。常用的集合型別 index by表 巢狀表可變陣列 index by表類似c語言中的陣列,但是元素順序並沒有按照特定的順序排列。元素的個數只受到binary integer的下標大小限制。type...

python中集合的使用

集合在學習和日常使用python過程中是必不可少的,下面介紹幾個常見的集合操作 集合 list 1 1,2,3,4,5,6,6,7,8,5,1,2 list 1 set list 1 list 2 set 2,6,0,66,22,8,4 print list 1,list 2 交集print lis...

python 中集合使用

集合 set 宣告集合 name set name 集合是用於儲存和處理資料的,常見的操作函式有增刪改 先刪除再新增 查 資料 in 集合名 下面詳細解說 add 增加資料 clear 清空 copy 複製 difference 兩個集合之間求差集,difference update 求差集並且用不...