【问题标题】:C# Dictionary ArrayList CountC# 字典 ArrayList 计数
【发布时间】:2011-05-19 15:06:04
【问题描述】:

有没有一种简单的方法来计算特定字典键值的值?

static void Main()
{
    Dictionary<string, ArrayList> SpecTimes = new Dictionary<string, ArrayList>;
    ArrayList times = new ArrayList();
    string count = "";

    times.Add = "000.00.00";
    times.Add = "000.00.00";
    times.Add = "000.00.00";

   string spec = "A101";

   SpecTimes.Add(spec,times);

   count = SpecTimes[spec].values.count;
}

【问题讨论】:

  • 这看起来像一半 C# 一半......我不知道。
  • 不要使用ArrayLists。相反,使用List&lt;T&gt;
  • 为什么要混合泛型集合和非泛型集合?并且不在您的方法调用中使用方括号?请出示真实代码。

标签: c# dictionary arraylist


【解决方案1】:

我没有测试过,但这应该接近你需要的。

static void Main()
{
  Dictionary<string, List<string>> SpecTimes = new Dictionary<string, List<string>>();
  List<string> times = new List<string>();
  int count = 0;

  times.Add = "000.00.00";
  times.Add = "000.00.00";
  times.Add = "000.00.00";

  string spec = "A101";

  SpecTimes.Add(spec,times);

  // check to make sure the key exists, otherwise you'll get an exception.
  if(SpecTimes.ContainsKey(spec))
  {
      count = SpecTimes[spec].Count;
  }
}

【讨论】:

    【解决方案2】:

    您的代码中有一些错误,因此无论如何它都不会编译。你应该这样改变它:

    static void Main()
    {
        IDictionary<string, IList<string>> specTimes = new Dictionary<string, IList<string>>();
        IList<string> times = new List<string>();
    
        times.Add("000.00.00");
        times.Add("000.00.00");
        times.Add("000.00.00");
    
        string spec = "A101";
        specTimes.Add(spec, times);
    
        int count = specTimes[spec].Count;
    }
    

    既然你已经知道出现的次数,那还有什么问题?

    【讨论】:

      【解决方案3】:

      您的代码不会按原样编译,您不应该使用 ArrayList,而是使用 List&lt;T&gt;(正如 SLaks 指出的那样。)话虽如此,List&lt;T&gt; 有一个 Count 属性,所以SpecTime[key].Count 应该可以正常工作(假设 key 实际上在字典中。)

      【讨论】:

      • 我知道你的意思是说Dictionary&lt;TKey, TValue&gt; 有一个Count 属性。
      • @Rick 确实如此,但我的意思是为什么?
      • 阅读速度太快。过失。 (SpecTime.CountSpecTime[key].Count
      【解决方案4】:

      如果您使用的是 .NET 3.5 及更高版本,请为此使用 Linq:

      var count = (from s in SpecTimes where SpecTimes.Key == <keyword> select s).Count();
      

      无论如何,正如大家所建议的,你应该选择List&lt;string&gt;而不是ArrayList

      【讨论】:

        【解决方案5】:

        如果使用 .NET 3.5,您可以使用 Linq 进行过滤和计数。但是尽可能避免使用 ArrayList,并使用泛型。

            static void Main(string[] args)
            {
                Dictionary<string, List<string>> SpecTimes = new Dictionary<string, List<string>>();
                List<string> times = new List<string>();
                int count;
        
                times.Add("000.00.00");
                times.Add("000.00.00");
                times.Add("000.00.00");
                times.Add("000.00.01");
        
                string spec = "A101";
        
                SpecTimes.Add(spec,times);
        
                // gives 4
                count = SpecTimes[spec].Count;
        
                // gives 3
                count = (from i in SpecTimes[spec] where i == "000.00.00" select i).Count();
        
                // gives 1
                count = (from i in SpecTimes[spec] where i == "000.00.01" select i).Count();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-16
          • 1970-01-01
          • 2016-02-04
          • 2016-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多