-1

辞書を反復処理しながら、キーと値のペアの列挙子の値を格納する方法があるかどうかを知りたいです。列挙子のキーと値をいくつかの変数に格納したいと思います。解決策は何ですか?私がやりたかったのは、辞書を反復処理して、現在のキーと値のペアと辞書内の次のキーと値のペアの参照を取得することです。なぜ機能しないのかわかりません。

ソリューションは次のようになります。

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);
        }
    }
}
4

5 に答える 5

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;
}
于 2013-02-18T18:13:01.620 に答える
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

于 2013-02-18T18:07:08.527 に答える
0

Iainや他のアプローチと同様に、次のようにすることもできます(Iainのように、これは現在と次ではなく前と現在を提供しますが、ほとんど同じです):

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;
                    }
                }
            }
        }
    }
于 2013-02-18T18:12:22.617 に答える
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を使用することもできます。

于 2013-02-18T18:12:42.083 に答える
0
List<KeyValuePair<Int32, Int32>> keyValuePairsWithIterations = new List<KeyValuePair<Int32, Int32>>();

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

これで、反復によってキーと値のペアにアクセスできます。しかし、それは少し強そうに見えます...そして、私はまだあなたがそれを必要としている理由を理解していません...

于 2013-02-18T18:13:29.600 に答える