在Dictionary / List中查找下一条记录

时间:2014-08-03 00:05:09

标签: c#

嘿,我用C#

创建的脚本有点问题

在下面附带的这个脚本中,我从一个对象收集战利品,这个脚本显示它。现在,例如,如果我想将项目[0]插入库存和项目[1],当这些项目不同时,脚本将覆盖清单中的该插槽。我需要做的是检查该插槽是否已满或与插槽中当前项目匹配的方法。我在[]上尝试了+1 ++ + =但它没有用。请帮助:)提前致谢

     // First Item
    if (GUILayout.Button(lootDictionary[0], GUILayout.Height(50)))
    {
        if (lootDictionary[0] != string.Empty && binlootamounts[0] != 0)
        {
            Inventory.inventoryDictionary[0] = lootDictionary[0];
            if (binlootamounts[0] != 0)
            {
                binlootamounts[0] -= 1;
                Inventory.dictionaryAmounts[0] += 1;
            }
        }
        if (binlootamounts[0] == 0)
        {
          lootDictionary[0] = string.Empty; 
        }

    }
    GUILayout.Label(binlootamounts[0].ToString(), GUILayout.Height(50));
    //Second Item
    if (GUILayout.Button(lootDictionary[1], GUILayout.Height(50)))
    {
        if (lootDictionary[1] != string.Empty && binlootamounts[1] != 0)
        {
            Inventory.inventoryDictionary[1] = lootDictionary[1];
            if (binlootamounts[1] != 0)
            {
                binlootamounts[1] -= 1;
                Inventory.dictionaryAmounts[1] += 1;
            }
        }
        if (binlootamounts[1] == 0)
        {
          lootDictionary[1] = string.Empty;
        }

    }

2 个答案:

答案 0 :(得分:1)

这是非常笼统的,可能不在轨道上,但没有更多细节,很难说。

我要说确保你的词典实现了一个界面,它可以比较它的对象,然后确保你的战利品对象继承它。

一个简单的例子如下(请注意,我没有尝试实际实现所有内容并确保其有效,因为我不知道您正在使用什么,但它&# 39;给你一个方向):

void Main()
{
    Dictionary<int,IMyLoot> MyLootDict = new Dictionary<int,IMyLoot>();
}

interface IMyLoot
{
    Loot GetLoot();
    // maybe you need something like this?
    bool CompareToOtherLoot();
}

class Loot {
    public int GoldValue { get; set; }
    public int Weight { get; set; }
}


class Weapon : Loot, IMyLoot {
    public string Name { get; set; }

    public Loot GetLoot() {return this;}

    public bool CompareToOtherLoot() {
        // Figure out how you compare to other loot
        return true;
    }
}

答案 1 :(得分:0)

如果您只需要支持快速访问(O(logN)或更好)和重复计数的通用集合,您可以创建类似以下内容的CountedSet<T>

public class CountedSet<T> : ICollection<T> 
{
    readonly IEqualityComparer<T> comparer;
    readonly Dictionary<T, int> dictionary;
    int totalCount = 0;
    int version = 1;

    public CountedSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
    {
        if (comparer == null)
            throw new ArgumentNullException("comparer");
        if (collection == null)
            collection = Enumerable.Empty<T>();
        this.comparer = comparer;
        int capacity;
        var coll = collection as ICollection<T>;
        if (coll != null)
            capacity = coll.Count;
        else
            capacity = 0;
        dictionary = new Dictionary<T, int>(capacity, comparer);
        totalCount = 0;
        foreach (var item in collection)
            Add(item);
    }

    public CountedSet(IEnumerable<T> collection)
        : this(collection, EqualityComparer<T>.Default)
    {
    }

    public CountedSet()
        : this(Enumerable.Empty<T>())
    {
    }

    int ComputeTotalCount()
    {
        int count = 0;
        foreach (var pair in dictionary)
            count += pair.Value;
        return count;
    }

    [Conditional("DEBUG")]
    void AssertValid()
    {
        Debug.Assert(totalCount >= 0, "totalCount >= 0");
        Debug.Assert(totalCount == ComputeTotalCount());
        foreach (var pair in dictionary)
            Debug.Assert(pair.Value > 0, "pair.Value > 0");
    }

    public void AddRange(IEnumerable<T> other)
    {
        foreach (var item in other)
            Add(item);
    }

    public void RemoveRange(IEnumerable<T> other) 
    {
        foreach (var item in other)
            Remove(item);
    }

    #region ICollection<T> Members

    public void Add(T item)
    {
        int itemCount;
        if (dictionary.TryGetValue(item, out itemCount))
        {
            Debug.Assert(itemCount > 0, "itemCount > 0");
            dictionary[item] = itemCount + 1;
        }
        else
        {
            dictionary[item] = 1;
        }
        version++;
        totalCount++;
        AssertValid();
    }

    public void Clear()
    {
        dictionary.Clear();
        totalCount = 0;
        version++;
    }

    public bool Contains(T item)
    {
        return dictionary.ContainsKey(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        foreach (var item in this)
            array[arrayIndex++] = item;
    }

    public int CountItem(T item)
    {
        int count;
        if (dictionary.TryGetValue(item, out count))
            return count;
        return 0;
    }

    public int Count { get { return totalCount; } }

    public bool IsReadOnly { get { return false; } }

    public bool Remove(T item)
    {
        int itemCount;
        if (dictionary.TryGetValue(item, out itemCount))
        {
            Debug.Assert(itemCount > 0, "itemCount > 0");
            itemCount--;
            if (itemCount < 1)
                dictionary.Remove(item);
            else
                dictionary[item] = itemCount;
            totalCount--;
            version++;
            AssertValid();
            return true;
        }
        else
        {
            return false;
        }
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
        int initialVersion = this.version;
        foreach (var pair in this.dictionary)
            for (int i = 0; i < pair.Value; i++)
            {
                if (initialVersion != this.version)
                {
                    throw new InvalidOperationException("collection was modified");
                }
                yield return pair.Key;
            }
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion
}

更一般地说,如果您要维护某种类型对象的内存数据库,您需要考虑维护它们的要求:

  1. 您是否需要每个项目数量的持久标识符 - 例如库存槽号?

  2. 如果添加和删除项目,持久数字如何变化?他们是否向上和向下调整以保持连续范围,或者是否存在差距?

  3. 添加到广告资源时是否会合并所有相同类型的对象,或者某些不合并?例如。黄金合并,但命名工件可能不会。

  4. 对象总量是否有限制?

  5. 无论您做出什么决定,您都可以采用上述方法,将一个或多个标准.Net集合封装到单个包装器集合中,以保持对数据的快速访问和正确计算。