集合

字典

预计阅读时间2 分钟 7 views

前言

Dictionary<TKey, TValue> 是一个泛型集合,由键值对组成,键值对的存储没有特定的顺序。字典中的键和值是根据键的类型和数据类型进行存储的。

作用

Dictionary<TKey, TValue> 用于存储和管理键值对数据,通过键快速查找对应的值。它适用于需要快速检索数据的场景。

使用场景

Dictionary<TKey, TValue> 适用于以下场景:

  1. 用户数据管理:例如存储用户的姓名和ID,通过ID快速检索用户信息。
  2. 配置项存储:如应用程序的设置项,通过设置名称快速获取配置值。
  3. 数据缓存:存储和快速检索计算结果或频繁使用的数据。

示例

  1. 创建字典
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典
        Dictionary<int, string> country = new Dictionary<int, string>();

        // 向字典中添加项
        country.Add(5, "巴西");
        country.Add(3, "中国");
        country.Add(4, "美国");

        // 打印键为 3 的值
        Console.WriteLine("键为 3 的值: " + country[3]);
    }
}

输出

键为 3 的值: 中国

在这个例子中,我们创建了一个字典 country,其中键是整数类型,值是字符串类型。然后,我们根据键访问并打印了一个值。

  1. 添加元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典
        Dictionary<string, string> mySongs = new Dictionary<string, string>();

        // 向字典中添加项
        mySongs.Add("五月天", "知足");
        mySongs.Add("陈奕迅", "爱情转移");
        mySongs.Add("林俊杰", "江南");
    }
}

在这个例子中,我们创建了一个名为 mySongs 的字典,并通过 Add() 方法添加了键值对。

  1. 访问字典元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典并添加元素
        Dictionary<string, string> student = new Dictionary<string, string>();

        // 向字典中添加项
        student.Add("姓名", "小明");
        student.Add("班级", "三年级");

        // 访问键为 "姓名" 的值
        Console.WriteLine(student["姓名"]);

        // 访问键为 "班级" 的值
        Console.WriteLine(student["班级"]);
    }
}

输出

小明
三年级

在这个例子中,我们通过键访问字典中的值。

  1. 遍历字典
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典并添加元素
        Dictionary<string, string> car = new Dictionary<string, string>();

        // 向字典中添加项
        car.Add("品牌", "宝马");
        car.Add("价格", "50万");

        // 遍历字典
        foreach (KeyValuePair<string, string> item in car)
        {
            Console.WriteLine("{0} : {1}", item.Key, item.Value);
        }
    }
}

输出

品牌 : 宝马
价格 : 50万

在这个例子中,我们使用 foreach 循环遍历字典中的键值对。

  1. 修改字典元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典并添加元素
        Dictionary<string, string> car = new Dictionary<string, string>();

        // 向字典中添加项
        car.Add("品牌", "宝马");
        car.Add("价格", "50万");

        // 打印原始值
        Console.WriteLine("修改前的品牌: " + car["品牌"]);

        // 修改 "品牌" 键的值为 "奔驰"
        car["品牌"] = "奔驰";

        // 打印更新后的值
        Console.WriteLine("修改后的品牌: " + car["品牌"]);
    }
}

输出

修改前的品牌: 宝马
修改后的品牌: 奔驰

在这个例子中,我们修改了字典中 “品牌” 键的值。

  1. 删除字典元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个字典并添加元素
        Dictionary<string, string> employee = new Dictionary<string, string>();

        // 向字典中添加项
        employee.Add("姓名", "王女士");
        employee.Add("职位", "主管");
        employee.Add("部门", "人事部");

        Console.WriteLine("原始字典:");

        // 遍历字典
        foreach (KeyValuePair<string, string> item in employee)
        {
            Console.WriteLine("{0} : {1}", item.Key, item.Value);
        }

        // 删除键为 "职位" 的元素
        employee.Remove("职位");

        Console.WriteLine("\n修改后的字典:");

        // 遍历修改后的字典
        foreach (KeyValuePair<string, string> item in employee)
        {
            Console.WriteLine("{0} : {1}", item.Key, item.Value);
        }
    }
}

输出

原始字典:
姓名 : 王女士
职位 : 主管
部门 : 人事部

修改后的字典:
姓名 : 王女士
部门 : 人事部

在这个例子中,我们删除了字典中键为 “职位” 的元素。

结语

Dictionary<TKey, TValue> 是一个强大的数据结构,适合用于需要快速查找、修改和删除键值对的场景。它提供了高效的操作方式来管理和访问数据。

Leave a Comment

分享此文档

字典

或复制链接

内容