0

だから、ポーションという辞書があります。ポーションのキーが、引数として渡されたオブジェクトと同じ名前かどうかを確認する必要があります。今はそれができますが、項目オブジェクトとキーが同じ名前の場合、その特定のキーに値を追加する方法がわかりません。このコードは、オブジェクトの最初のインスタンスに対して正常に機能します。しかし、キーと同じ名前の別のインスタンス オブジェクトを追加すると、キーが見つからないという例外が発生します。2 つのオブジェクトが同じではないことは理解しています。辞書内のオブジェクト参照を抽出するにはどうすればよいですか? それとも別の方法がありますか?

public static void addItem(Potion item)
 {
    if (Potions.Count >0)
    {
        foreach(KeyValuePair<Potion,int> pair in Potions)
        {
            if (pair.Key.itemName == item.itemName)
            {
            containsItem = true;
            }   
        }
        if (containsItem)
        {
        Potions[item] += 1;
        Debug.Log (Potions[item]);
        containsItem = false;
        }
        else
            {       
            Potions.Add(item,1);
            }
    }
    else
    {
    Potions.Add (item,1);
    }

    foreach(KeyValuePair<Potion,int> pair in Potions)
    {
        Debug.Log (pair.Key.itemName + " : " + pair.Value);
    }
 }
4

4 に答える 4

3

私は実際に別の実装を提供します。

enum Potion
{
    Health,
    Mana
}

class PotionBag
{
    readonly int[] _potions = new int[Enum.GetValues(typeof(Potion)).Length];

    public void Add(Potion potion)
    {
        _potions[(int)potion]++;
    }

    public void Use(Potion potion)
    {
        if (GetCount(potion) == 0)
            throw new InvalidOperationException();
        _potions[(int)potion]--;
    }

    public int GetCount(Potion potion)
    {
        return _potions[(int)potion];
    }
}
于 2012-08-31T03:08:36.107 に答える
1

キーとして使用Potionしていますが、コードによると、重要なのはitemName. したがって、辞書を に変更することをお勧めします<string, int>。また、コメントしたように、カスタム クラスを使用する場合は、オーバーライドすることをお勧めしEqualsますGetHashCode

コードは次のようになります。

 public static void addItem(Potion item)
 {
    if(Potions.ContainsKey(item.itemName))
        Potions[item.itemName] += 1;
    else
        Potions.Add (item.itemName,1);

    foreach(KeyValuePair<string,int> pair in Potions)
    {
        Console.WriteLine(pair.Key + " : " + pair.Value);
    }
 }
于 2012-08-31T03:09:17.870 に答える
1

追加するアイテムをキーとして使用していて、同じオブジェクトではないため、これは機能しません。

キーをプレースホルダーに保存して、ループの後で探してみませんか?

Potion key = null;
foreach(KeyValuePair<Potion,int> pair in Potions)
    {
        if (pair.Key.itemName == item.itemName)
        {
         key = pair.Key
        }   
    }

if(key != null):
    Potions[key] += 1
于 2012-08-31T03:04:28.933 に答える
1

EqualsとをオーバーライドできますGetHashCodeが、それには他の意味がある場合があります。代わりにIEqualityComparer、辞書を作成するときに次のように使用できます。

class Potion {
    public string Name;
    public int Color;
}

class PotionNameEqualityComparer : IEqualityComparer<Potion> {
    public bool Equals(Potion p1, Potion p2) {
        return p1.Name.Equals(p2.Name);
    }
    public int GetHashCode(Potion p1) {
        return p1.Name.GetHashCode();
    }
}

void Main() {
    var d = new Dictionary<Potion, int>(new PotionNameEqualityComparer());
    var p1 = new Potion() { Name = "Health", Color = 1 };
    var p2 = new Potion() { Name = "Health", Color = 2 };
    d.Add(p1, 1);
    d[p2]++; // works, and now you have two health potions. 
             // Of course, the actual instance in the dictionary is p1; 
             // p2 is not stored in the dictionary.    
}           
于 2012-08-31T03:24:41.853 に答える