【问题标题】:How to compare sequence of two string array如何比较两个字符串数组的序列
【发布时间】:2012-10-03 07:38:57
【问题描述】:

我有两个字符串数组

string[] a; 
string[] b; 

如何发现数组a中的元素序列与数组b相似?

因为我使用的是 .net2.0,所以我不能使用 LINQ。

编辑

这是我尝试过的

foreach (string val2 in a)
                { 
                                       foreach (string val1 in b)
                    {
                        if (val1 == val2)
                        {
                           // Values are same - continue processing
                        }
                        else
                        {
                            // Value not same   -- exit                         }
                    }
                } 

【问题讨论】:

  • “元素序列”到底是什么?
  • 定义相似。您是否在每个数组中的相同索引之间寻找相同的匹配项?
  • 展示一个例子,说明你拥有什么以及你期望的结果。

标签: c# arrays c#-2.0


【解决方案1】:
    private bool Compare(string[] a, string[] b)
    {
        if (a.Length != b.Length) return false;

        for (int i = 0; i< a.Length; i++)
            if (a[i] != b[i]) return false;

        return true;
    }

【讨论】:

    【解决方案2】:

    我相信您想看看两个数组中的元素是否具有相同的值和相同的索引。你可以有一个简单的方法,比如:

    public static bool IsSequenceEqual(string[] a, string[] b)
    {
        if (a.Length != b.Length)
            return false;
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] != b[i])
                return false;
        }
        return true;
    }
    

    【讨论】:

      【解决方案3】:

      如果两个列表都已排序,则可以使用其他解决方案之一。但如果列表未排序,您可以执行以下操作。

        Dictionary<string, byte> dict = new Dictionary<string, byte>();
        foreach (string s in a)
        {
            dict.Add(s, 0);
        }
      
        bool same = true;
        foreach (string s in b)
        {
            same &= dict.ContainsKey(s);
            if (!same) 
                break;
        }
      

      您仍然可以在此之前进行简单的测试,例如检查等长等。

      如果您可以使用 .NET 4.0,则可以使用 Intersect 数组方法。 然后您可以执行a.Intersect(b) 并将长度与ab 的长度进行比较。如果那是true,那么列表是相等的,因为两个数组的所有元素都相交。您可以使用a.Union(b) 进行类似的操作。

      编辑 按照 phoog 的建议,使用 Dictionary 而不是 ArrayList。

      【讨论】:

      • 你为什么使用ArrayList而不是List&lt;string&gt;?无论如何,Contains 在列表中是 O(n);如果列表很大,最好使用HashSet&lt;&gt;,其中Contains 是O(1)。 (HashSet&lt;&gt; 是在 .NET 3.5 中引入的,因此对于 .NET 2.0 或 3.0,您可以使用 Dictionary&lt;string, byte&gt;,忽略值,并使用 ContainsKey 而不是 Contains。)
      • 我很快就采纳了你的建议。自从我不得不在 .NET 2.0 中做任何事情已经很长时间了,所以我不记得我们做过和没有做过的每一件事。显然,使用较新版本的 .NET 可以做得更好。
      【解决方案4】:
          /// <summary>
          /// Compare two string array. 
          ///  If both null - return true ; 
          ///  If only one is null - return false ;
          /// </summary>
          bool IsSequenceEqual(string[] a, string[] b)
          {
              if (a == null && b == null) // both null - return true
                  return true;
              if (a == null || b == null) // only one is null - return false
                  return false;
      
              if (a.Length != b.Length) // length is different - return false
                  return false;
      
              // check if equal
              for (int i = 0; i < a.Length; i++)
              {
                  if (a[i] != b[i])
                      return false;
              }
              return true;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-09
        • 2020-04-24
        • 1970-01-01
        • 2022-06-29
        • 2018-09-26
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        相关资源
        最近更新 更多