C#,入门教程(35)——哈希表(Hashtable)的基础知识与用法

news2024/9/24 5:33:13

上一篇:

C#,入门教程(34)——关于函数的参数之引用(ref)的一点知识与源程序icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/125411351

有一段故事:

King Log

The frogs in the lake had an easy life doing exactly what they wanted. But what pleased one frog annoyed another, and they could see things could be better. All the frogs agreed they needed a strong leader to make the rules they should live by. So they sent a message to the King of all animals. "Very well," said the King, and threw a log into a lake, telling the frogs this was their new leader.

At first the frogs were terrified of it.

When it splashed into the lake they all dived to the bottom and hid in mud. But after a while, when the log did nothing but float on the surface, they lost their fear. They hopped all over it and carried on as before. They sent another message to the King saying they needed a better one.

"Then you must learn your lesson," said the King. This time he sent a water snake, who took one look at all the frogs and ate as many of them as it could catch.

问题是:

(1)这段文字中有 lesson 这个词吗?

(2)frogs 这个词出现了几次?

(3)frogs 这个词在哪些位置出现?

用程序解答以上问题,最方便与快速的,莫过于 哈希表 Hashtable 了。

哈希表Hashtable是一种常用数据集合。

仔细读一读下面的代码,你基本上可以知道怎么用了。

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;


// 1、将故事文字组成一个字符串
StringBuilder sb = new StringBuilder();
sb.AppendLine("King Log");
sb.AppendLine("The frogs in the lake had an easy life doing exactly what they wanted. But what pleased one frog annoyed another, and they could see things could be better. All the frogs agreed they needed a strong leader to make the rules they should live by. So they sent a message to the King of all animals. \"Very well,\" said the King, and threw a log into a lake, telling the frogs this was their new leader.");
sb.AppendLine("At first the frogs were terrified of it.");
sb.AppendLine("When it splashed into the lake they all dived to the bottom and hid in mud. But after a while, when the log did nothing but float on the surface, they lost their fear. They hopped all over it and carried on as before. They sent another message to the King saying they needed a better one.");
sb.AppendLine("\"Then you must learn your lesson,\" said the King. This time he sent a water snake, who took one look at all the frogs and ate as many of them as it could catch.");

// 2、字符串按空格,分隔成字(表)
string[] words = sb.ToString().Split(' ');

// 3、创建一个保存字、位置信息的哈希表
Hashtable hash = new Hashtable();

// 4、字的相对位置
int offset = 0;
foreach (string word in words)
{
	// 如果哈希表中,尚未保存 word 信息
	if (!hash.ContainsKey(word))
	{
		// 添加一个字、位置(列表)的信息;
		hash.Add(word, new List<int>() { offset });
	}
	else
	{
		// 获取已经保存的位置列表信息,加入新的位置
		List<int> list = (List<int>)hash[word];
		list.Add(offset);
	}
	// 位置偏移量往后增加 word 的长度,和1空格;
	offset += word.Length + 1;
}

// 清除 StringBuilder 的原有信息,后面用于显示计算结果
sb.Clear();
// 回答题目中的三个问题
sb.AppendLine("1. Has word 'lesson' ? " + hash.ContainsKey("lesson") + "<br>");
List<int> fc= (List<int>)hash["frogs"];
sb.AppendLine("2. Word 'frogs' appears " + fc.Count + " times.<br>");
sb.AppendLine("3. Word 'frogs' appears at " + String.Join(",", fc.ToArray()) + " <br>");
// 答案显示于 webBrowser1 组件。
webBrowser1.DocumentText = sb.ToString();

代码不长,作为初学者,如果能把每句话都读懂,就能进一大步。

作业:

怎么能搜索某个词在第几行(或那几行)出现?

 ——————————————————————

POWER BY 315SOFT.COM &
TRUFFER.CN

下一篇:

C#,入门教程(36)——尝试(try)捕捉(catch)不同异常(Exception)的点滴知识与源代码icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/124271911

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1404142.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

神经网络算法与逻辑回归:优势与差异

神经网络算法和逻辑回归都是预测模型中的重要工具&#xff0c;但它们在处理复杂和非线性问题时表现出不同的性能。本文将深入探讨神经网络算法相对于逻辑回归的优势&#xff0c;以及它们在不同场景下的适用性。 一、引言 神经网络算法和逻辑回归都是预测模型中的重要工具&…

C# CefSharp 根据输入日期段自动选择日期

前言 搞这个Demo整整搞几天通宵&#xff0c;爆肝了。后做的效果出来&#xff0c;还是不错的。给小伙伴看看效果图。 2, 遇到的问题 日期之间相差多少个月数。开始时间框点击对应月份要点击多少次&#xff0c;结束时间框点击对应月份要点击多少次Xpath获取问题。找到对应html元…

力扣刷题第七天 分割数组的最大值

给定一个非负整数数组 nums 和一个整数 k &#xff0c;你需要将这个数组分成 k 个非空的连续子数组。 设计一个算法使得这 k 个子数组各自和的最大值最小。 注&#xff1a; 1 < nums.length < 10000 < nums[i] < 1061 < k < min(50, nums.length) 示例一 输入…

cdh6.3.2的hive配udf

背景 大数据平台的租户要使用udf&#xff0c;他们用beeline连接&#xff0c; 意味着要通过hs2&#xff0c;但如果有多个hs2&#xff0c;各个hs2之间不能共享&#xff0c;需要先把文件传到hdfs&#xff0c;然后手动在各hs2上create function。之后就可以永久使用了&#xff0c;…

宠物互联网医院系统

在数字时代&#xff0c;宠物医疗迎来了一场革新&#xff0c;动物互联网医院系统以其先进的技术和智能的特性成为宠物护理的领军者。本文将介绍宠物互联网医院系统的一些关键技术和代码示例&#xff0c;揭示这一科技奇迹的实现原理。 1. 远程医疗服务的实现 远程医疗服务是宠…

<蓝桥杯软件赛>零基础备赛20周--第15周--快速幂+素数

报名明年4月蓝桥杯软件赛的同学们&#xff0c;如果你是大一零基础&#xff0c;目前懵懂中&#xff0c;不知该怎么办&#xff0c;可以看看本博客系列&#xff1a;备赛20周合集 20周的完整安排请点击&#xff1a;20周计划 每周发1个博客&#xff0c;共20周。 在QQ群上交流答疑&am…

Spring-配置文件

一、引子 了解完Spring的基本概念后&#xff0c;我们紧接着来了解Spring中的核心文件--Spring配置文件。 二、配置Bean 我们在上一节Spring的基本概念中快速使用了一下Spring&#xff0c;其中我们在配置文件中主要涉及到就是Bean标签的配置&#xff1a;主要的配置字段有id, …

[设计模式Java实现附plantuml源码~创建型] 对象的克隆~原型模式

前言&#xff1a; 为什么之前写过Golang 版的设计模式&#xff0c;还在重新写Java 版&#xff1f; 答&#xff1a;因为对于我而言&#xff0c;当然也希望对正在学习的大伙有帮助。Java作为一门纯面向对象的语言&#xff0c;更适合用于学习设计模式。 为什么类图要附上uml 因为很…

C++ //练习 2.36 关于下面的代码,请指出每一个变量的类型以及程序结束时它们各自的值。

C Primer&#xff08;第5版&#xff09; 练习 2.36 练习 2.36 关于下面的代码&#xff0c;请指出每一个变量的类型以及程序结束时它们各自的值。 int a 3, b 4; decltype(a) c a; decltype((b)) d a; c; d;环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09;…

基本语法和 package 与 jar

3.基本语法 1.输入输出 // 导入 java.util 包中的 Scanner 类 import java.util.Scanner;// 定义名为 ScannerExample 的公共类 public class ScannerExample {// 主方法&#xff0c;程序的入口点public static void main(String[] args) {// 创建 Scanner 对象&#xff0c;用…

分布式websocket即时通信(IM)系统保证消息可靠性【第八期】

b站上面本期视频版本&#xff0c;观看视频食用更佳&#xff01;点击即可跳转,找不到视频可以直接搜索我 目前叫 呆呆呆呆梦 目前已经写的文章有。并且有对应视频版本。 git项目地址 【IM即时通信系统&#xff08;企聊聊&#xff09;】点击可跳转 sprinboot单体项目升级成sprin…

2017年认证杯SPSSPRO杯数学建模A题(第二阶段)安全的后视镜全过程文档及程序

2017年认证杯SPSSPRO杯数学建模 A题 安全的后视镜 原题再现&#xff1a; 汽车后视镜的视野对行车安全非常重要。一般来说&#xff0c;汽车的后视镜需要有良好的视野范围&#xff0c;以便驾驶员能够全面地了解车后方的道路情况。同时&#xff0c;后视镜也要使图像的畸变尽可能…

【Linux】—— 共享内存

本期我将要带大家学习的是有关进程间通信的另一种方式——共享内存。共享内存是一种用于进程间通信的高效机制&#xff0c;允许多个进程访问和操作同一块内存区域。 目录 &#xff08;一&#xff09;深刻理解共享内存 1.1 概念解释 1.2 共享内存原理 1.3 共享内存数据结构 …

【5G 接口协议】N2接口协议NGAP(NG Application Protocol)介绍

博主未授权任何人或组织机构转载博主任何原创文章&#xff0c;感谢各位对原创的支持&#xff01; 博主链接 本人就职于国际知名终端厂商&#xff0c;负责modem芯片研发。 在5G早期负责终端数据业务层、核心网相关的开发工作&#xff0c;目前牵头6G算力网络技术标准研究。 博客…

web开发学习笔记(13.mybatis基于注解配置)

1.使用mybatis基本步骤 2.引入依赖 <!-- mysql--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency> <!-- mybatis--><dependency><groupId>org…

如何在Docker上运行Redis

环境: 1.windows系统下的Docker deckstop 1.Pull Redis镜像 2.运行Redis镜像 此时,Redis已经启动&#xff0c;我们登录IDEA查看下是否连接上了 显示连接成功&#xff0c;证明已经连接上Docker上的Redis了

制冷系统几种节流方式介绍

1.两次节流、中间气液分离 通过两个阶段的节流达到制冷剂的减压&#xff1a;通过第一毛细管&#xff0c;使液体制冷剂膨胀到中间压力后&#xff0c;由汽液分离器将气态制冷剂和液态制冷剂分离&#xff0c;液态制冷剂通过第二毛细管进一步膨胀&#xff08;减压&#xff09;&…

(十二)Head first design patterns代理模式(c++)

代理模式 代理模式&#xff1a;创建一个proxy对象&#xff0c;并为这个对象提供替身或者占位符以对这个对象进行控制。 典型例子&#xff1a;智能指针... 例子&#xff1a;比如说有一个talk接口&#xff0c;所有的people需要实现talk接口。但有些人有唱歌技能。不能在talk接…

一款自动化提权工具

免责声明 请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;作者不为此承担任何责任。工具来自网络&#xff0c;安全性自测&#xff0c;如有侵权请联系删除。…

vue:element-ui表单动态验证规则

一、需求&#xff1a; 实现当是否发送消息选择是时&#xff0c;业务类型字段必填。 二、实现&#xff1a; 当你在一个表单中使用 el-form 和 el-form-item 来创建表单项时&#xff0c;el-form-item 的 :rules 属性可以用来设置该表单项的验证规则。我们希望根据用户在 "…