因此,我有两个带有名称的数组,我想检查一下,给定两个名称,它们是否分别位于一个数组中且处于相同位置。例如,如果我具有名称“ Mike”和“ Charles”以及数组[“ John”,“ Mike”,“ Sophie”]和[“ Ellie”,“ Charles”,“ Ryan”],则返回true。
我正在制作一个“秘密圣诞老人”程序,我不希望夫妻彼此像“秘密圣诞老人”一样。
答案 0 :(得分:4)
Array.IndexOf
听起来像您要找的东西。
string[] firstArray = new string[] { "John", "Mike", "Sophie" };
string[] secondArray = new string[] { "Ellie", "Charles", "Ryan" };
string firstName = "Mike";
string secondName = "Charles";
if (!firstArray.Contains(firstName) || !secondArray.Contains(secondName))
return false;
return Array.IndexOf(firstArray, firstName) == Array.IndexOf(secondArray, secondName);
答案 1 :(得分:0)
string[] firstArray = new string[] { "John", "Mike", "Sophie" };
string[] secondArray = new string[] { "Ellie", "Charles", "Ryan" };
string firstName = "Sophie";
string secondName = "Ryan";
return (Array.IndexOf(firstArray, firstName) == Array.IndexOf(secondArray,
secondName));
答案 2 :(得分:0)
您可以尝试这个。
var firstArray = new string[] { "John", "Mike", "Sophie" };
var secondArray = new string[] { "Ellie", "Charles", "Ryan" };
var firstName = "Mike";
var secondName = "Charles";
var firstNameIndex = firstArray.ToList().FindIndex(a => a == firstName);
var secondNameIndex = secondArray.ToList().FindIndex(a => a == secondName);
if (firstNameIndex == secondNameIndex && (firstNameIndex != -1 || secondNameIndex != -1))
{
return true;
}
else
{
return false;
}
答案 3 :(得分:0)
您可以尝试以下操作
var index1 = Array.IndexOf(new[] { "John", "Mike", "Sophie" }, "Mike");
var index2 = Array.IndexOf(new[] { "Ellie", "Charles", "Ryan" } , "Charles");
return index1 == index2 && index1 != -1;