代码之家  ›  专栏  ›  技术社区  ›  iburlakov

循环通过system.collections.generic.dictionary via for语句

  •  6
  • iburlakov  · 技术社区  · 16 年前

    我有个简短的问题。有简单的循环方式吗 System.Collections.Generic.Dictionary 通过 for C中的声明

    事先谢谢。

    9 回复  |  直到 11 年前
        1
  •  7
  •   Fredrik Mörk    16 年前

    不合理,不,你 能够 使用LINQ扩展 ElementAt :

    for (int i = 0; i < dictionary.Keys.Count; i++)
    {
        Console.WriteLine(dictionary.ElementAt(i).Value);                
    }
    

    …但我真的不明白这一点。就用普通的吧 foreach 方法。如果出于某种原因,在迭代时需要跟踪索引,那么可以“在边上”这样做:

    int index = 0;
    foreach (var item in dictionary)
    {
        Console.WriteLine(string.Format("[{0}] - {1}", index, item.Value));
    
        // increment the index
        index++;
    }
    
        2
  •  11
  •   Philippe Leybaert    16 年前

    您可以使用foreach:

    Dictionary<string,string> dictionary = new Dictionary<string,string>();
    
    // ...
    
    foreach (KeyValuePair<string,string> kv in dictionary) 
    {
        string key = kv.Key;
        string value = kv.Value;
    }
    
        3
  •  3
  •   Oxymoron    16 年前

    有几种方法。 循环键:

    foreach(var key in myDictionary.Keys)
    

    循环值:

    foreach(var value in myDic.Values)
    

    循环通过对:

    foreach(KeyValuePair<K, V> p in myDic)
    {  
         var key = p.Key;
         var value = p.Value
    }
    
        4
  •  0
  •   Andrey Shchekin    16 年前

    不, for 主要用于具有索引的集合。 你可以 foreach 但是,很容易克服。

        5
  •  0
  •   ChrisBD    16 年前

    如果您使用的是NET 3.5或更高版本,那么可以使用LINQ和Precate来定位特定的值。 字典不一定按顺序存储它们的键值对(按条目顺序或键顺序)。

        6
  •  0
  •   Keith    16 年前

    Philippe准备好了 foreach 尽管我通常将其简化为:

    foreach (var pair in dictionary) 
    {
        var key = pair.Key;
        var value = pair.Value;
    }
    

    无法使用 for 循环,因为它们没有按顺序存储。但是,您可以将键或值作为集合进行循环。

        7
  •  0
  •   LaustN    16 年前

    这是可以做到的,但在我看来有点傻

            Dictionary<string,string> dictionary = new Dictionary<string, string>();
            Dictionary<string, string>.Enumerator enumerator = dictionary.GetEnumerator();
            for (int i = 0; i < attributeValues.Count;i++ )
            {
                KeyValuePair<string, string> current = enumerator.Current;
                //do something usefull
                enumerator.MoveNext();
            }
    

    唯一能得到的是一个(相当无用的)指数,如果这是实际目标,那么这样的指数能更好地为您服务:

            int currentIndex = 0;
            foreach (KeyValuePair<string, string> keyValuePair in dictionary)
            {
                //do something usefull
                currentIndex++;
            }
    
        8
  •  -1
  •   Matt Greer    16 年前

    你可以把钥匙圈起来

     for(int i = 0; i < myDictionary.Keys.Length; ++i)
          myDictionary.Keys[i] ...
    

    或价值观

     for(int i = 0; i < myDictionary.Values.Length; ++i)
          myDictionary.Values[i] ...
    

    或者两个都是菲利普展示的

        9
  •  -1
  •   user4531    11 年前

    如果它不必是一个“for”循环,可以使用 Dictionary.GetEnumerator Method 使用“while”循环是一个选项-它也非常简单 伊姆霍 :

    var enumerator = d.GetEnumerator();
    while (enumerator.MoveNext())
    {
    var pair = enumerator.Current;
    b += pair.Value;
    }
    enumerator.Dispose();
    

    代码段来自 C# Dictionary GetEnumerator