【问题标题】:How to have a function return both index and value of an array?如何让函数返回数组的索引和值?
【发布时间】:2018-11-13 03:32:55
【问题描述】:

此函数计算给定字符串中字母出现的频率并将其放入数组中(索引是字母的 ascii 数和值是计数的出现次数)。现在我需要返回字母(它已经返回)和值。仅通过在线阅读,我无法弄清楚如何使用 ref 和替代品来做到这一点。

static char MostCommonLetter(string s)
    {
        int[] occurrances = new int[255];
        for (int i = 0; i < s.Length; i++)
        {
            if (char.IsLetter(s[i]))
            {
                int ascii = (int)s[i];
                occurrances[ascii]++;
            }
        }
        char maxValue = (char)Array.IndexOf(occurrances, occurrances.Max());
        return maxValue;
    }

【问题讨论】:

    标签: c# return tuples ref out


    【解决方案1】:

    另一种解决方案是使用 LINQ:

    string str = "Hello World!";
    
    var result = str.GroupBy(c => c)
                    .Select(group => new { Letter = group.Key, Count = group.Count() })
                    .OrderByDescending(x => x.Count)
                    .First();
    
    char letter = result.Letter;
    int count = result.Count; 
    

    字母 = 'l'

    count = 3

    【讨论】:

    • 啊,是的,没看过……但现在我不能删除反对票,除非你编辑答案,StackOverflow 说。
    • 已删除反对票。对不起。 ;)
    【解决方案2】:

    在 C# 中执行所需操作的最佳且灵活的方法是使用 struct

    以这种方式定义一个结构并使用它同时返回多个结果(一个结构甚至可以包含函数……您可以将这些结构视为更轻的类):

    namespace YourApp.AnyNamespace {
    
        // Other things
    
        public struct SampleName
        {
            public char mostCommon;
            public int occurancies;
        }
    }
    

    【讨论】:

    • 这肯定不是“最好和灵活的方式”
    • @SlavenTojić 无论如何,另一个用户提醒了元组,它是在 C# 7 中添加的,它更易于使用。
    【解决方案3】:

    您可以使用“out”参数从函数中返回附加参数。

        static char MostCommonLetter(string s, out int maxOccurrance)
        {
            int[] occurrances = new int[255];
            for (int i = 0; i < s.Length; i++)
            {
                if (char.IsLetter(s[i]))
                {
                    int ascii = (int)s[i];
                    occurrances[ascii]++;
                }
            }
    
            maxOccurrance = occurrances.Max();
            char maxValue = (char)Array.IndexOf(occurrances, maxOccurrance);
    
            return maxValue;
        }
    
        //...
    
        // In C# 7 and above you can call it like that
        var c = MostCommonLetter("abccd", out int maxOccurrance);
    
        //// In older version of C# you should just declare out variable before use it
        //int maxOccurrance;
        //var c = MostCommonLetter("abccd", out maxOccurrance);
    

    【讨论】:

      【解决方案4】:

      在 C# 7 及更高版本中,Value Tuples 是您的最佳选择。您可以按如下方式定义您的函数:

      static (char letter, int occurrences) MostCommonLetter(string s)
      {
          int[] occurrences = new int[255];
          for (int i = 0; i < s.Length; i++)
          {
              if (char.IsLetter(s[i]))
              {
                  int ascii = (int)s[i];
                  occurrances[ascii]++;
              }
          }
          char letter = (char)Array.IndexOf(occurrences, occurrences.Max());
          return (index: letter, occurrences: occurrences);
      }
      

      然后您可以像这样引用输出:

      var (index, occurrences) = MostCommonLetter(yourString);
      

      【讨论】:

        猜你喜欢
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-15
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多