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

上一篇:

C#,入门教程(34)------关于函数的参数之引用(ref)的一点知识与源程序https://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是一种常用数据集合。

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

cs 复制代码
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)的点滴知识与源代码https://blog.csdn.net/beijinghorn/article/details/124271911

相关推荐
向宇it14 分钟前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
yngsqq1 小时前
一键打断线(根据相交点打断)——CAD c# 二次开发
windows·microsoft·c#
TENET信条2 小时前
day53 第十一章:图论part04
开发语言·c#·图论
anlog3 小时前
C#在自定义事件里传递数据
开发语言·c#·自定义事件
向宇it4 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
仰望大佬0075 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#
糖朝5 小时前
c#读取json
c#·json
向宇it10 小时前
【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接口里氏替换原则、密封方法(`sealed` )
java·开发语言·unity·c#·游戏引擎·里氏替换原则
Java Fans14 小时前
C# 中串口读取问题及解决方案
开发语言·c#
盛派网络小助手14 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#