2

http://users.metropolia.fi/~dangm/blog/?p=67で説明されている問題の解決策を実装しようとしています。私は c# 言語を初めて使用します。列挙子を使用して、特定の条件で辞書を反復処理したいと考えています。そのため、current と previous.current の 2 つの変数があり、辞書の最初の要素を指します。previous は辞書の前の要素を指します。辞書私はフォローのように繰り返しています

previous=current;
current.MoveNext();

問題は、辞書全体の前のポイントを辞書の最後の要素に、現在のポイントをランダムなキーと値のペア RawVariable(0,0) に初めて反復するときです。ディクショナリで、特定のキーまたは値を持つ要素を現在のポイントにする方法

ここに私のコードスニペットがあります

 public void falling_disks(int[] A, int[] B)
    {
        Dictionary<int, int> filledDictionary = filldictionary(d1, A);
        //previous stores the previous element in dictionary
        var previous = filledDictionary .GetEnumerator();
        //current stores next element of previous
        var current = filledDictionary .GetEnumerator();
        current.MoveNext();

        //for each incoming element in array B
        foreach (int ele in B)
        {

            //check if the current key is filled in hashtable h1 that is check if it
            //is already added
            if (!checkifthatvalueisfilled(current.Current.Key))
            {
                //if not check if current value is less than or equal to element
                while ((current.Current.Value >= ele))
                {
                    //assign previous to current
                    previous = current;
                    //move current to next position
                    current.MoveNext();
                }
                listofitemstoremove.Add(previous.Current.Key);

            }
            else
            {
                listofitemstoremove.Add(current.Current.Key);
            }

            foreach (int item in listofitemstoremove)
            {
                if (!(h1.ContainsKey(item)))
                    h1.Add(item, true);
            }

        }
        Console.WriteLine(listofitemstoremove.Capacity);
    }

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

}
4

3 に答える 3

0

私があなたの質問を正しく理解したなら、あなたはそれをすることができません。列挙子を使用すると、コレクションに連続してアクセスできます。これがポイントです。最初からその要素を繰り返さずに、突然特定の要素に移動することはできません。

さらに、列挙子を使用する正当な理由は1つもありません。アルゴリズムの前の要素と現在の要素への参照が必要な場合は、列挙子ではなく、それらのキーを保存する必要があります。また、これらの行はかなり確信しています

 while ((current.Current.Value >= ele))
            {
                //assign previous to current
                previous = current;
                //move current to next position
                current.MoveNext();
            }

a)コレクションの最後に到達すると、例外がスローされますb)参照型を割り当てているため、期待どおりに機能しません

于 2013-03-07T07:40:34.257 に答える
0

あなたの質問を理解しているかどうかわかりませんが、おそらくこれを変更したいでしょう:

                previous = current;

これに:

                previous.MoveNext();

そうすれば、「前」は常に「現在」の一歩後ろになります。元のコードで行った方法で変数を割り当てると、「現在の」オブジェクトへの参照が 2 つだけになり、インクリメントされます。

于 2013-03-07T08:16:55.037 に答える
0

あなたの質問はわかりにくいです。おそらくこれは、ループの最初にやりたいことですか?

current = h1.GetEnumerator();
current.MoveNext();
于 2013-03-07T04:44:52.640 に答える