如果你有一个像这样的数组列表:
List<int[]> ListOfArrays = new List<int[]>();
ListOfArrays.Add(new int[] { 1, 1 });
ListOfArrays.Add(new int[] { 2, 1 });
如何找到列表中 { 2, 1} 的索引?
我不想使用迭代循环。我想要一种简洁的方法,就像 PaRiMaL RaJ 在 Check if string array exists in list of string 中建议的那样。 :
list.Select(ar2 => arr.All(ar2.Contains)).FirstOrDefault();
(如果给定字符串数组的成员存在于字符串数组列表中,以上代码将返回 true)
最佳答案
var myArr = new int[] { 2, 1 };
List<int[]> ListOfArrays = new List<int[]>();
ListOfArrays.Add(new int[] { 1, 1 });
ListOfArrays.Add(new int[] { 4, 1 });
ListOfArrays.Add(new int[] { 1, 1 });
ListOfArrays.Add(new int[] { 2, 1 });
int index = ListOfArrays.FindIndex(l => Enumerable.SequenceEqual(myArr, l));
关于c# - 在 C# 的数组列表中查找数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451417/