【问题标题】:How can i compare arrays in two jagged arrays C#我如何比较两个锯齿状数组中的数组 C#
【发布时间】:2017-03-24 13:25:02
【问题描述】:

好的,所以我有 2 个锯齿状数组,我想遍历它们并将每个数组与另一个锯齿状数组中的每个数组进行比较。 所以在这个例子中,我想在 access[1] == visual[0] 时返回 true。

但似乎我无法明确比较锯齿状数组中的数组。我怎样才能引用整个数组,而不仅仅是这些数组中的元素? 我的意思是如果我写access[0][0],我会得到"10"。但我不能写access[0] 得到"10","16"

string[][] access = new string[][] {
                    new string[] {"10","16"},
                    new string[] {"100","20"},
                    new string[] {"1010","2"},
                    new string[] {"1011","1"}
                };

string[][] visual = new string[][] {
                new string[] {"100","20"},
                new string[] {"101","36"},
                new string[] {"101","37"},
                new string[] {"101","38"}
            };

【问题讨论】:

  • 您的问题存在于同一级别:您不能直接比较两个string[] 数组...您必须逐个元素地比较它们...
  • 但我不能写 access[0] 来获得 "10","16"... 以同样的方式,使用一维数组 string[] access = { "10, 16" } ,你不能写access来得到"10", "16"

标签: c# arrays compare comparison jagged-arrays


【解决方案1】:

但我不能写 access[0] 来获得 "10","16"

你可以。但是要比较你需要使用的元素Enumerable.SequenceEqual

if (Enumerable.SequenceEqual(access[1], visual[0])) { ... }

【讨论】:

  • 找到匹配后如何删除access[i] 的数组有什么想法吗?
  • 我会使用List<string[]> 而不是string[][],这样您就可以访问List.RemoveAt
  • 好的,谢谢。我会这样做的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2015-09-23
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多