在C#中搜索字符串数组列表的有效方法

Sud*_*ha 1 c# search

我有这样的结构,

String[] variable1= new String["ABC", "FSS" , "FSFS", "GDGDDS"];
String[] variable2= new String["SA", "GS" , "QE", "HF"];


static List<String[]> allList = new List<String[]>();;

allList .Add(variable1);
allList .Add(variable2);
Run Code Online (Sandbox Code Playgroud)

当String提供a时,我想搜索allList 并提供结果,如果它找到了哪个数组.

有效存档的任何帮助?

Dir*_*aio 5

两者都提供了以线性时间运行的解决方案,如果你有很多单词并且进行大量查询,这将会太慢.

你可以使用字典.字典在内部使用哈希表,它会更快,更快.

要将所有字符串放在字典中,您可以执行以下操作:

Dictionary<String, String[]> dict = new Dictionary<String, String[]>();
foreach(String[] arr in allList)
    foreach(String str in arr)
        dict[str] = arr;
Run Code Online (Sandbox Code Playgroud)

然后你可以轻松搜索它:

String s = "ABC";
if(dict.ContainsKey(s))
    // result is dict[s]
else
    // String is not in any array
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!