C#基础(17)——Hashtable

1、Hashtable简介

Hashtable称为键值对集合,类似于Python的字典 ,根据键去找值的。
这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HashTable集合
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add(1, "张三");
            ht.Add(false, "错误");
            ht.Add(2, "s3");
            //var推断类型,必须赋初值
            //C#是一门强类型语言,在代码中必须对每一个变量的类型进行明确的定义
            //js是一门弱类型语言,不需要定义类型
            foreach (var item in ht.Keys)//遍历键
            {//效率高于for循环 
                Console.WriteLine("{0}--->{1}",item,ht[item]);
            }
            Console.ReadKey();
        }
    }
}

2、键值对的添加

键必须唯一,值可以重复,添加方式:

 Hashtable ht = new Hashtable();
            ht.Add(1, "张三");
            ht.Add(false, "错误");
            ht.Add(2, "s3");
            ht[6] = "新来的";

添加已有的,Add会报错,而修改操作:
这里写图片描述

3、判断已有的键

 Hashtable ht = new Hashtable();
            ht.Add(1, "张三");
            ht.Add(false, "错误");
            ht.Add(2, "s3");
            ht[6] = "新来的";
            ht[6] = "把新来的干掉";
            //var推断类型,必须赋初值
            //C#是一门强类型语言,在代码中必须对每一个变量的类型进行明确的定义
            //js是一门弱类型语言,不需要定义类型
            if (!ht.ContainsKey(2))//不包含时
            {
                ht.Add(2, "顶顶顶顶");
            }
            else
            {
                Console.WriteLine("已经包含这个key键");
            }

清空操作:

ht.Clear

删除操作:

ht.Remove(2);

4、Hashtable练习

大写转换为小写:
这里写图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HashTable集合
{
    class Program
    {
        private const string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private const string alpha = "abcdefghijklmnopqrstuvwxyz";
        private static string str;
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            for (int i = 0; i < Alpha.Length; i++)
            {
                ht[Alpha[i]] = alpha[i];
            }

            //foreach (var item in ht.Keys)
            //{
            //    Console.WriteLine("{0}-->{1}", item, ht[item]);
            //}

            Console.WriteLine("请输入大写字母:");
            string input = Console.ReadLine();
            for (int i = 0; i < input.Length; i++)
            {
                if (ht.ContainsKey(input[i]))//一定要字符input[i],不是字符串
                {
                    str += ht[input[i]].ToString();
                }
                else
                {
                    str += input[i];
                }
            }

            Console.WriteLine(">>>{0}", str);


            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值