c# - 无法使用 [] 将索引应用于“System.Collections.Generic.HashSet”类型的表达式'
问题描述
我有两个哈希集。一个由字符串列表组成,另一个是字符串数组列表。
HashSet<string> hashSetpdf = new HashSet<string>();
var hashSetReports = new HashSet<string[]>(DTtoList(dReport));
所以我试图从 hashSetpdf 到 hashSetReports 的所有元素的第一个索引中搜索一个字符串。
实际上,我的以下代码都运行良好,但我的问题是,如果我有很多数据,它会花费很多时间。
foreach (string c in hashSetpdf)
{
if (hashSetReports.Any(r => r.Contains(c)))
{
//do something...
}
else
{
//do something...
}
}
我尝试了以下方法,但它给了我一个错误。
foreach (string c in hashSetpdf)
{
if (hashSetReports.Contains(c))
{
//do something...
}
else
{
//do something...
}
}
这是我遇到的错误。
Error 5 Cannot apply indexing with [] to an expression of type
'System.Collections.Generic.HashSet<string[]>'
有没有什么快速的方法来搜索哈希集中字符串数组的第一个索引?
解决方案
因此,您有一组字符串(称为 A),并且您想检查在一组字符串列表(称为集合 B)中,该集合中任何列表中的任何字符串是否具有字符串在您的一组字符串 (A) 中。
这必然需要|A| * \sum_i |B| * n_i 次,其中 n_i 是集合 B 中第 i 个列表的长度,|X| 表示 X 的基数。
推荐阅读
- c++ - 使用嵌入式 Python 检查 C++ 中的内存泄漏
- javascript - ReactJS ..event 处理程序适用于其中一种道具,但不是全部
- python - tkinter 帧中没有空格的 Matplotlib 绘图
- android - Using two spaces to place an object in the middle of the screen in a horizontal linearlayout
- python - Merge two data frames on three columns in Python
- powershell - Post JSON object via Powershell
- python - Django 上的待处理请求
- python - How to send data from python client to Django web server?
- python - Set attribue in custom class derived from uuid.UUID: AttributeError
- java - (Java) Static member accessed via instance reference with enumerators