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

在C.NET 2.0中,反向做前臂的简单方法是什么?

  •  16
  • Pandincus  · 技术社区  · 18 年前

    假设我有一个字典对象:

    Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
    

    现在我想按相反的顺序遍历字典。我不能使用简单的for循环,因为我不知道字典的键。一 前额 很容易:

    foreach (SomeObject object in myDictionary.Values)
    {
        // Do stuff to object
    }
    

    但我怎么能反向执行呢?

    14 回复  |  直到 18 年前
        1
  •  19
  •   Jonathan    18 年前

    SortedList sCol = new SortedList();
    
    sCol.Add("bee", "Some extended string matching bee");
    sCol.Add("ay", "value matching ay");
    sCol.Add("cee", "Just a standard cee");
    
    // Go through it backwards.
    for (int i = sCol.Count - 1; i >=0 ; i--)
        Console.WriteLine("sCol[" + i.ToString() + "] = " + sCol.GetByIndex(i));
    
    // Reference By Key
    foreach (string i in sCol.Keys)
        Console.WriteLine("sCol[" + i + "] = " + sCol[i]);
    
    // Enumerate all values
    foreach (string i in sCol.Values)
        Console.WriteLine(i);
    

        2
  •  28
  •   leppie    18 年前

        3
  •  18
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    foeach (SomeObject o in myDictionary.Values.Reverse())
    {
         // Do stuff to object
    }
    
        4
  •  4
  •   Jason Plank Maksim Kondratyuk    14 年前

    static IEnumerable<T> CreateReverseIterator<T>(IList<T> list)
    {
        int count = list.Count;
        for (int i = count - 1; i >= 0; --i)
        {
            yield return list[i];
        }
    }
    

    static void Main(string[] args)
    {
        Dictionary<int, string> dict = new Dictionary<int, string>();
    
        dict[1] = "value1";
        dict[2] = "value2";
        dict[3] = "value3";
    
        foreach (KeyValuePair<int, string> item in dict)
        {
            Console.WriteLine("Key : {0}, Value: {1}", new object[] { item.Key, item.Value });
        }
    
        string[] values = new string[dict.Values.Count];
        dict.Values.CopyTo(values, 0);
    
        foreach (string value in CreateReverseIterator(values))
        {
            Console.WriteLine("Value: {0}", value);
        }
    
    }
    

        5
  •  3
  •   OwenP    18 年前

    foreach

    for (int i = myCollection.Length - 1; i >= 0; i--)
    {
        // do something
    }
        6
  •  3
  •   to StackOverflow    18 年前

    public static IEnumerable<T> Reverse<T>(IEnumerable<T> items)
    {
        IList<T> list = items as IList<T>;
        if (list == null) list = new List<T>(items);
        for (int i = list.Count - 1; i >= 0; i-- )
        {
            yield return list[i];
        }
    }
    
        7
  •  1
  •   Jason Plank Maksim Kondratyuk    14 年前

    Dictionary<int, SomeObject> myDictionary

    foreach(SomeObject _object in myDictionary.Values.Reverse())
    {
    }
    
        8
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    Dictionary<int, object> d;
    List<object> tmplist;
    foreach (object o in d.Values) tmplist.Add(s);
    tmplist.Reverse();
    foreach (object o in tmplist) {
        //Do stuff
    }
    
        9
  •  0
  •   Timothy Carter    18 年前

        10
  •  0
  •   Gord    18 年前
        11
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    for

        13
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    Dictionary<int, SomeObject>  myDictionary = new Dictionary<int, SomeObject>();
    
    foreach (var pair in myDictionary.OrderByDescending(i => i.Key))
    {
        //Observe pair.Key
        //Do stuff to pair.Value
    }
    
        14
  •  -2
  •   Marvin    14 年前
    foreach (Sample in Samples)
    
    try the following:
    
    Int32 nEndingSample = Samples.Count - 1;
    
    for (i = nEndingSample; i >= 0; i--)
    {
         x = Samples[i].x;
         y = Samples[i].y;
    }