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

发布时间:2024年01月22日

上一篇:

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

文章来源:https://blog.csdn.net/beijinghorn/article/details/124236243
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。