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

相关推荐
-凌凌漆-几秒前
【C#】async与await介绍
开发语言·c#
君莫愁。22 分钟前
【Unity】搭建基于字典(Dictionary)和泛型列表(List)的音频系统
数据结构·unity·c#·游戏引擎·音频
计算机学姐1 小时前
基于Asp.net的教学管理系统
vue.js·windows·后端·sqlserver·c#·asp.net·visual studio
且听风吟ayan1 小时前
leetcode day26 重复的子字符串
算法·leetcode·c#
caoruipeng3 小时前
Windows编程----进程的当前目录
c++·windows·c#
h20170106874 小时前
C#中的委托是什么?事件是不是一种委托?委托与事件的区别?
开发语言·c#·.net·面试题
无所谓จุ๊บ4 小时前
使用AI整理知识点--WPF动画核心知识
c#·wpf
hihaojie5 小时前
C# 中的“相等判断”
c#·总结
Whappy0015 小时前
第三节:基于Winform框架的串口助手小项目---串口操作《C#编程》
linux·单片机·c#
kylezhao20195 小时前
C# Excel开源操作库MiniExcel使用教程
开发语言·c#·excel