当前位置: 首页 > news >正文

Delphi数据字典TDictionary

在 Delphi 中,创建一个数据字典通常意味着使用一种结构来存储键值对。Delphi 没有内建的字典类型,但你可以使用 TStringList 从 Classes 单元作为一个简单的键值对存储,或者你可以使用更复杂的第三方容器,如 TDictionary 从 Generics.Collections 单元。

以下是一个使用 TDictionary 的简单示例:

usesSystem.SysUtils, System.Generics.Collections;varDict: TDictionary<string, string>;Key: string;Value: string;
begin// 创建一个新的字典实例Dict := TDictionary<string, string>.Create;try// 添加一些键值对Dict.Add('Key1', 'Value1');Dict.Add('Key2', 'Value2');Dict.Add('Key3', 'Value3');// 遍历字典并输出键值对for Key in Dict.Keys dobeginValue := Dict[Key];WriteLn(Format('Key: %s, Value: %s', [Key, Value]));end;// 检查一个特定的键是否存在,并输出其值if Dict.ContainsKey('Key2') thenWriteLn(Format('Value for Key2: %s', [Dict['Key2']]))elseWriteLn('Key2 not found');// 移除一个键值对Dict.Remove('Key3');// 再次检查已移除的键是否存在if Dict.ContainsKey('Key3') thenWriteLn('Key3 still exists')elseWriteLn('Key3 has been removed');finally// 释放字典实例Dict.Free;end;
end;

uses
  System.SysUtils, System.Generics.Collections;

var
  Dict: TDictionary<string, string>;
  Key: string;
  Value: string;
begin
  // 创建一个新的字典实例
  Dict := TDictionary<string, string>.Create;
  try
    // 添加一些键值对
    Dict.Add('Key1', 'Value1');
    Dict.Add('Key2', 'Value2');
    Dict.Add('Key3', 'Value3');

    // 遍历字典并输出键值对
    for Key in Dict.Keys do
    begin
      Value := Dict[Key];
      WriteLn(Format('Key: %s, Value: %s', [Key, Value]));
    end;

    // 检查一个特定的键是否存在,并输出其值
    if Dict.ContainsKey('Key2') then
      WriteLn(Format('Value for Key2: %s', [Dict['Key2']]))
    else
      WriteLn('Key2 not found');

    // 移除一个键值对
    Dict.Remove('Key3');

    // 再次检查已移除的键是否存在
    if Dict.ContainsKey('Key3') then
      WriteLn('Key3 still exists')
    else
      WriteLn('Key3 has been removed');
  finally
    // 释放字典实例
    Dict.Free;
  end;
end;

http://www.lryc.cn/news/465235.html

相关文章:

  • VsCode 如何自定义代码片段(Code Snippet)
  • Linux服务器前后端项目部署vue+springboot—搭建服务器上的运行环境(JDK、Redis、MySQL、Nginx)
  • 随记:有关idea中jdk版本
  • 【算法篇】贪心类(1)(笔记)
  • el-select 可搜索、多选状态遮挡住搜索框
  • el-table中实现可选表格区域的鼠标事件检测
  • 特种作业操作登高架设作业历年真题附答案
  • schedule-执行周期性任务
  • python图片文件路径排序
  • ARTTrack6/M、ARTTrack5、ART AT7-80横向对比
  • 【Vue】Vue3.0(十三)中标签属性ref(加在普通标签上、加在组件标签上)、局部样式
  • Linux系统基础-文件系统
  • 机器学习和深度学习常用的工具库
  • 【unity小技巧】Unity6 LTS版本安装和一些修改和新功能使用介绍
  • 5种边界填充
  • 鸿蒙网络编程系列7-TLS安全数据传输单向认证示例
  • LangGraph 源码分析 | BaseTool 模板类
  • vulnhub靶场之JOY
  • intel和AMD突然联姻,这操作给我看傻了
  • yolo_face_pose-DataBall 人脸关键点数据集 >> DataBall
  • Unity 山水树木
  • Redis 性能优化选择:Pika 的配置与使用详解
  • 【某农业大学计算机网络实验报告】实验三 IP数据报发送和转发流程
  • Android13 添加运行时权限
  • 官方操刀占用仅6G,Win 11 LTSC详细安装、优化教程来了
  • 【论文精读】RELIEF: Reinforcement Learning Empowered Graph Feature Prompt Tuning
  • 2023-06 GESP C++三级试卷
  • Maven--简略
  • leetcode 刷题day44动态规划Part13( 647. 回文子串、516.最长回文子序列)
  • 华为OD机试真题---关联子串