自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 中字典 Dictionary 詳解

開(kāi)發(fā) 前端
Dictionary<TKey, TValue>?是C#中用于存儲(chǔ)鍵值對(duì)集合的泛型類(lèi),屬于System.Collections.Generic?命名空間。它允許使用鍵(Key?)來(lái)訪問(wèn)與其關(guān)聯(lián)的值(Value)。其中,TKey表示字典中鍵的類(lèi)型,TValue表示字典中值的類(lèi)型。

基本概念

Dictionary<TKey, TValue>是C#中用于存儲(chǔ)鍵值對(duì)集合的泛型類(lèi),屬于System.Collections.Generic命名空間。它允許使用鍵(Key)來(lái)訪問(wèn)與其關(guān)聯(lián)的值(Value)。其中,TKey表示字典中鍵的類(lèi)型,TValue表示字典中值的類(lèi)型。

Dictionary的基本結(jié)構(gòu)

  • 鍵(Key):唯一標(biāo)識(shí)集合中的一個(gè)元素。鍵是唯一的,不能有重復(fù)。
  • 值(Value):與鍵相關(guān)聯(lián)的數(shù)據(jù)。值可以是任意類(lèi)型,并且可以有重復(fù)。
  • 鍵值對(duì)(KeyValuePair):鍵和值的組合,表示Dictionary中的一個(gè)元素。

Dictionary的主要特性

  • 快速訪問(wèn):通過(guò)鍵可以快速檢索到對(duì)應(yīng)的值,平均時(shí)間復(fù)雜度接近O(1),因?yàn)镈ictionary<TKey,TValue>類(lèi)是作為哈希表實(shí)現(xiàn)。
  • 唯一鍵(Key):每個(gè)鍵在Dictionary中都是唯一的,不能重復(fù)。
  • 動(dòng)態(tài)大?。篋ictionary的大小可以動(dòng)態(tài)調(diào)整,當(dāng)元素?cái)?shù)量超過(guò)容量時(shí),它會(huì)自動(dòng)擴(kuò)容。
  • 無(wú)序集合:Dictionary中的元素是無(wú)序的,不能通過(guò)索引來(lái)訪問(wèn)它們。

Dictionary的常用操作

以下是C#中Dictionary的常用操作完整代碼,其中包括添加元素、訪問(wèn)元素、修改元素、刪除元素、檢查鍵或值是否存在,以及遍歷元素:

public static void DictionaryOperation()
{
    //創(chuàng)建一個(gè)Dictionary來(lái)存儲(chǔ)學(xué)生學(xué)號(hào)ID和姓名
    Dictionary<int, string> studentDic = new Dictionary<int, string>();

    #region 添加元素

    // Add方法(鍵必須唯一)
    studentDic.Add(1, "大姚");
    studentDic.Add(2, "小袁");
    studentDic.Add(3, "Edwin");

    // 索引器語(yǔ)法(鍵不存在時(shí)添加,存在時(shí)更新)
    studentDic[4] = "Charlie";
    studentDic[5] = "追逐時(shí)光者";

    // 安全添加(避免異常)
    bool isAdded = studentDic.TryAdd(6, "小明"); // 返回 false,因鍵已存在

    #endregion

    #region 訪問(wèn)元素

    // 直接訪問(wèn)(鍵必須存在,否則會(huì)有異常)
    var currentUserName = studentDic[1];
    Console.WriteLine($"當(dāng)前學(xué)生姓名: {currentUserName}");

    // 安全訪問(wèn)(避免異常)
    if (studentDic.TryGetValue(5, outvar getUserName))
    {
        Console.WriteLine($"UserName:{getUserName}");
    }
    else
    {
        Console.WriteLine("當(dāng)前學(xué)生ID不存在");
    }

    #endregion

    #region

    // 修改元素
    studentDic[2] = "大西瓜";

    Console.WriteLine($"修改后的名稱(chēng):{studentDic[2]}");

    #endregion

    #region 刪除元素

    // 刪除元素
    bool isRemoved = studentDic.Remove(3);

    Console.WriteLine($"刪除結(jié)果:{isRemoved}");

    #endregion

    #region 檢查鍵或值是否存在

    // 檢查鍵是否存在
    if (studentDic.ContainsKey(1))
    {
        Console.WriteLine("存在");
    }
    else
    {
        Console.WriteLine("不存在");
    }

    bool isExistcontainsValue = studentDic.ContainsValue("追逐時(shí)光者");

    Console.WriteLine($"是否存在:{isExistcontainsValue}");


    #endregion

    #region 遍歷元素

    // 遍歷元素
    foreach (KeyValuePair<int, string> student in studentDic)
    {
        Console.WriteLine($"ID: {student.Key}, Name: {student.Value}");
    }

    // 使用鍵的枚舉器
    foreach (var key in studentDic.Keys)
    {
        Console.WriteLine($"Key: {key}, Value: {studentDic[key]}");
    }

    // 使用值的枚舉器
    foreach (varvaluein studentDic.Values)
    {
        // 注意:這種方式不能直接獲取鍵,只能獲取值
        Console.WriteLine($"Value: {value}");
    }

    #endregion
}

參考文章

  • https://learn.microsoft.com/zh-cn/dotnet/api/system.collections.generic.dictionary-2?view=net-9.0
責(zé)任編輯:武曉燕 來(lái)源: 追逐時(shí)光者
相關(guān)推薦

2010-03-15 17:56:24

Python字典

2009-05-27 10:12:27

LINQ泛型字典Dictionary

2024-03-12 10:25:14

C#Dictionary編程語(yǔ)言

2020-11-01 17:01:00

Python字典開(kāi)發(fā)

2010-03-03 10:50:22

Python字典應(yīng)用方

2024-11-15 00:09:21

2021-08-25 11:25:41

MySQLData Dictio數(shù)據(jù)庫(kù)

2021-08-26 10:44:31

MySQL MySQL Data 阿里云

2024-12-19 09:00:00

字典視圖對(duì)象Python

2010-03-15 16:34:50

Python字典

2015-07-28 10:06:03

C#內(nèi)部實(shí)現(xiàn)剖析

2024-04-24 11:27:16

字典推導(dǎo)式Python

2024-11-21 12:00:00

字典緩存Python

2010-05-10 15:22:34

Oracle數(shù)據(jù)字典

2025-02-11 09:49:12

2024-02-06 09:53:45

Pythonget()函數(shù)Dictionary

2024-11-21 09:00:00

Python字典代碼

2021-06-12 09:39:50

Python字典數(shù)據(jù)類(lèi)型Python基礎(chǔ)

2023-12-12 13:55:00

Pythonsubprocess命令

2010-03-15 16:54:11

Python字典
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)