2013-02-18 86 views
-1

我想知道是否有任何方法在遍歷字典時存儲鍵值對的枚舉器的值。我想將枚舉數的鍵和值存儲在某個變量中。解決辦法是什麼?我想要做的是當迭代通過字典有一個當前鍵值對和字典中的下一個鍵值對的參考。我不知道爲什麼它不工作c中的詞典迭代器#

這是什麼解決方案可能看起來像:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections; 
using System.Diagnostics; 

namespace WellPuzzle 
{ 

    class Solution 
    { 
     Hashtable h1 = new Hashtable(); 
     List<int> listofitemstoremove = new List<int>(); 

     Dictionary<int, int> d1 = new Dictionary<int, int>(); 

     public void falling_disks(int[] A, int[] B) 
     { 
      var itemstoremove = new List<int>(); 

      var en = d1.GetEnumerator(); 
      int count = 0; 
      for (int i = 0; i <= A.Length - 1; i++) 
      { 
       d1.Add(count++, A[i]); 
      } 
      //for each incoming element in array 
      foreach (int ele in B) 
      { 
       //store prev as current position of enumerator 
       var prev = new KeyValuePair<int, int>(); 
       prev = en.Current; 
       //check if it is possible to iterate to next element in dictionary 
       if (en.MoveNext()) 
       { 
        //loop till end of dictionary 
        while (en.MoveNext()) 
        { 
         //if current value of enumerator in dictionary is less than incoming            element and check if corroesponding key for that value is in hashtable or not 
         if (en.Current.Value <= ele && !(checkifthatvalueisfilled(en.Current.Key))) 
          continue; 
         else 
         {//if current enumerator value is greater than incoming element from array B then remove all elements from prev reference till end of dictionary 
          h1.Add(en.Current.Key, true); 
          listofitemstoremove.Add(en.Current.Key); 
         } 
         prev = en.Current; 
        } 

        if (!(h1.ContainsKey(en.Current.Key))) 
        { 
         h1.Add(en.Current.Key, true); 
         listofitemstoremove.Add(en.Current.Key); 
        } 
       } 
       else 
       { 
        h1.Add(prev.Key, true); 
        listofitemstoremove.Add(prev.Key); 
       } 
       foreach (int item in listofitemstoremove) 
       { 
        for (int i = item; i < d1.Count; i++) 
        { 
         d1.Remove(i++); 
        } 
       } 
      } 

      Console.WriteLine(h1.Count); 
     } 






     public bool checkifthatvalueisfilled(int value) 
     { 
      if (h1.ContainsValue(h1.ContainsKey(value)) == true) 
       return true; 
      else return false; 
     } 
    } 


    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] A = new int[] { 5, 6, 4, 3, 6, 2, 3 }; 
      int[] B = new int[] { 2, 3 }; 
      Solution s1 = new Solution(); 
      s1.falling_disks(A, B); 
     } 
    } 
} 
+3

你的代碼爲書面?當然不是。你有'en'鍵入到用於存儲KeyValuePair的IEnumerator中,並且'next'試圖存儲void返回方法的結果。你究竟在努力完成什麼?最終,而不是手段。 – 2013-02-18 18:05:10

+1

看起來你試圖把字典當作具有按序枚舉器的東西(或者完全按順序)。不要這樣做。根據你想要的,你看過一個多維'int [,]'數組是否可以工作嗎? – Earlz 2013-02-18 18:07:04

+0

它看起來像你試圖設置枚舉器的當前位置,就像你將在C++中設置一個指針一樣。如果這就是你想要的,那在C#中是不可能的;您只能將枚舉數移至下一個項目;你不能把它設置成任意的項目。 – Servy 2013-02-18 18:07:11

回答

1

有沒有你不能使用一個很好的理由: -

// Replace TKey and TValue with the types from the dictionary 
TKey previousKey; 
TValue previousValue; 

bool first = true; 

foreach(var key in dictionary.Keys) 
{ 
    var value = dictionary[key]; 

    if (!first) 
    { 
    ... // Do whatever you need to do with the keys and values 
    } 

    previousKey = key; 
    previousValue = value; 
    first = false; 
} 

(請注意,雖然,你可能不得不.OrderBy(...).Keys爲此作出任何意義)

0

除了伊恩的和其他的方法,你也可以做這樣的(像伊恩的,這給以往和當前,而不是目前和未來,但他們幾乎是同樣的事情):

using System; 
using System.Collections.Generic; 

namespace Demo 
{ 
    public static class Program 
    { 
     private static void Main(string[] args) 
     { 
      var d1 = new Dictionary<int, int> {{1, 1}, {2, 2}, {3, 3}, {4, 4}}; 
      bool isFirst = true; 
      var previous = new KeyValuePair<int, int>(); 

      foreach (var current in d1) 
      { 
       if (!isFirst) 
       { 
        // You have current and previous available now. 
        Console.WriteLine("Current = " + current.Value + ", previous = " + previous.Value); 
       } 

       previous = current; 
       isFirst = false; 
      } 
     } 
    } 
} 

以下是您如何通過手動使用調查員來完成的:

using System; 
    using System.Collections.Generic; 

    namespace Demo 
    { 
     public static class Program 
     { 
      private static void Main(string[] args) 
      { 
       var d1 = new Dictionary<int, int> {{1, 1}, {2, 2}, {3, 3}, {4, 4}}; 
       var iter = d1.GetEnumerator(); 

       if (iter.MoveNext()) 
       { 
        var previous = iter.Current; 

        while (iter.MoveNext()) 
        { 
         // You have current and previous available now. 
         Console.WriteLine("Current = " + iter.Current.Value + ", previous = " + previous.Value); 
         previous = iter.Current; 
        } 
       } 
      } 
     } 
    } 
0

爲什麼你需要一個迭代器? foreach循環會爲你做到這一點。

如果需要當前和以前的項目,你可以只存儲上一個項目在每次迭代:

Dictionary<int, int> d1 = new Dictionary<int, int>(); 
KeyValuePair<int, int> previous = null; 
KeyValuePair<int, int> current = null; 
foreach (KeyValuePair<int, int> item in d1) 
{ 
    previous = current; 
    current = item; 
    // do what you need to do with previous and current 
} 

你也可以使用一個SortedDictionary,會給你一個索引。

+0

在那裏使用未初始化的值... – 2013-02-18 18:18:32

+0

正確。如果爲null,則表示您沒有以前的項目。 – gabnaim 2013-02-18 21:54:23

+0

我編輯答案初始化爲空。 – gabnaim 2013-02-18 22:03:33

2

看起來你希望能夠訪問先前的值以及序列的當前值。下面是接受一個序列,並把它變成一個代表每個值與它的原來的順序前值對的序列的簡單的輔助方法:

public static IEnumerable<Tuple<T, T>> GroupAdjacent<T>(IEnumerable<T> source) 
{ 
    using (var iterator = source.GetEnumerator()) 
    { 
     if (!iterator.MoveNext()) 
     { 
      yield break; 
     } 
     T previous = iterator.Current; 

     while (iterator.MoveNext()) 
     { 
      yield return Tuple.Create(previous, iterator.Current); 
     } 
    } 
} 

它可能然後被使用,如:

foreach(var pair in GroupAdjacent(dictionary)) 
{ 
    var previous = pair.Item1; 
    var current = pair.Item2; 
} 
0
List<KeyValuePair<Int32, Int32>> keyValuePairsWithIterations = new List<KeyValuePair<Int32, Int32>>(); 

foreach(var keyvaluepair in d1) 
{ 
    keyValuePairsWithIterations.Add(keyvaluepair); 
} 

現在您可以通過迭代訪問您的keyvaluepairs。但它看起來有點強烈...而我仍然不明白你需要它...