0

lb1、lb2、lb3 の 3 つの ListBox があります。

lb1 には 4 つの要素があり、lb2 には 5 つの要素があるとしましょう。

(lb1 と lb2) のそれぞれの一意の組み合わせを lb3 の要素に割り当てることができます。

コレクションに保存したい組み合わせと関連付け。私の最初の方法は、Key = (lb1Element1_lb2Element1)、Value = lb3Element1 で KeyValuePair を使用することでした。

しかし、このソリューションでは問題が発生します。lb1Element1 を削除するとします。KeyValuePair-List から lb1Element1 が発生する他のすべての組み合わせを削除するオプション (?) はありません。

この場合、どのコレクション タイプが最適でしょうか?

前もってありがとうジョン

編集: 3 つの ListBoxes にはすべて数字が含まれています。

4

3 に答える 3

1

lb1 用に 1 つ、lb2 用に 1 つの 2 つの辞書はどうでしょうか。

Dictionary<string, Dictionary<string,string>>

最初の dic: キーは各 lb1 値であり、値は lb2 のすべての値です (キーと値が同じ辞書) 2 番目の dic: キーは各 lb2 値であり、値は lb1 のすべての値です

lb2 リスト ボックスからオプション "x" を削除した場合、削除された lb2 値に接続されているすべての lb1 値を見つけるには、1 番目の dic から "x" を lb2 値として持つすべてのペアを削除し、"x" 全体を削除します。 " 2 番目の dic のキー:

Foreach(var lb1value in Dic2.ElementAt("x").value.keys)
  {
    Dic1.ElementAt("lb1value").
     value.RemoveAt("x");
  }

dic2.removeAt("x");
于 2013-01-07T22:40:11.333 に答える
1

You could just use a Dictionary<string,string> for keyvalue which also provides the ability to Remove()

 Dictionary<string, string> items = new Dictionary<string, string>();

 items.Remove("mykey");
于 2013-01-07T22:37:41.100 に答える
0

キーのクラスを作成しない理由:

public class YourKey
{
    public int Item1 { get; private set; }
    public int Item2 { get; private set; }

    public YourKey(int item1, int item2)
    {
       this.Item1 = item1;
       this.Item2 = item2;
    }

    public override bool Equals(object obj)
    {
        YourKey temp = obj as YourKey;
        if (temp !=null)
        {
            return temp.Item1 == this.Item1 && temp.Item2 == this.Item2;
        }
        return false;
    }

    public override int GetHashCode()
    {
        int hash = 37;
        hash = hash * 31 + Item1;
        hash = hash * 31 + Item2;
        return hash;
    }
}

次に、これを a で使用して、Dictionary<YourKey, int>すべての値を格納できます。

これには、Item1 と Item2 の組み合わせごとに 1 つの値しか格納できないという利点があります。

項目 1 == 1 を持つ yourDictionary 内のすべてのエントリを削除する場合:

var entriesToDelete = yourDictionary.Where(kvp => kvp.Key.Item1 == 1).ToList();
foreach (KeyValuePair<YourKey, int> item in entriesToDelete)
{
    yourDictionary.Remove(item.Key);
}
于 2013-01-07T22:58:04.767 に答える