我有两个单词列表。
List<string> mainList = new List<string> {"blue", "green", "mother", "black", "gray"};
List<string> checkList = new List<string> {"mother", "green", "father", "black", "gray"};
然后我想从第一个列表中取一个随机元素......
Random rand = new Random();
string iGenerated = mainList[rand.Next(mainList.Count)];
然后检查此字符串是否也属于第二个列表。我不确定我究竟能做到这一点。我想到了这样的......这是正确的方法吗?
if checkList.Contains(iGenerated) bool strInArray = true;
else bool strInArray = false;
答案 0 :(得分:1)
以下是我在Console应用程序中的程序:
char buf[128];
count(buf, file_name);
printf("%s", buf);
答案 1 :(得分:1)
使用Contains()
的{{1}}
List
结果:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> mainList = new List<string> { "blue", "green", "mother", "black", "gray" };
List<string> checkList = new List<string> { "mother", "green", "father", "black", "gray" };
Random r = new Random();
// Run five random tests
for (int i = 0; i < 5; i++)
{
string mainListItem = mainList[r.Next(0, mainList.Count)];
Console.WriteLine(checkList.Contains(mainListItem)
? "{0} found in checkList"
: "{0} not found in checkList", mainListItem);
}
}
}