我是新来的,对 C# 不太熟悉。我一直在通过 MSDN 文档和 Google 进行搜索,但找不到这个问题的答案(我尝试尽可能笼统地说):
我想在列表或数组中存储一个固定长度的有序整数序列,然后将这些整数数组组成一个数组。有谁知道我如何计算不同整数数组的数量,以及我应该使用哪种特定数据类型(列表、普通数组等)?我没有我一直在使用的确切代码,但这里有一些类似于我一直在尝试的代码:
int[] set1 = {2, 56, 8};
int[] set2 = {8, 25, 90};
int[] set3 = {2, 56, 8};
var superset = new List<int[]>;
superset.Add(set1);
superset.Add(set2);
superset.Add(set3);
Console.Out.WriteLine(superset.Distinct().Count()); // would like this to output 2, but Distinct() doesn't seem to actually work and I would get 3
最佳答案
Distinct
方法有一个重载,它采用 IEqualityComparer 的实例。 .为 int 数组(即 public class IntArrayComparer : IEqualityComparer<int[]>
)创建 IEqualityComparer 的实现,并将实例传递到对 Distinct 的调用中。
SequenceEqual方法可能对 IEqualityComparer<int[]>.Equals
的实现有所帮助但是这个练习留给你了。
关于c# - .NET C# - 数组中不同数组的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8733215/