C#比较两个列表和返回值

时间:2017-10-04 12:03:56

标签: c# list unity3d compare

我有问题。我尝试比较两个列表 currentItemsInColl bPList 。在 bPList 里面,我有其他列表 RequiredItems ,现在就是我需要的。 我想比较 currentItemsInColl RequiredItems 并返回bPList.craftingBlueprint。 我尝试比较,但我不知道如何使用它:/

using Devdog.InventoryPro;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class CraftingAutoUpdate : MonoBehaviour {

    public ItemCollectionBase itemCollection;
    public ItemCollectionBase rewardCollection;
    public CraftingCategory craftingCategory;

    [Header("Blue Print List")]

    public List<BlueprintList> bPList = new List<BlueprintList>();
    public List<CurrentItemInCollList> currentItemsInColl = new List<CurrentItemInCollList>();

    private CraftingBlueprint readyBlueprint;

    public void OnShow()
    {
        GetBluePrint();
        InvokeRepeating("StartUpdate",0f,0.05f);
    }

    public void OnHide()
    {
        CancelInvoke("StartUpdate");
    }

    private void StartUpdate()
    {

        UpdateDirectory();
        UpdateFindMatchItems();
        UpdateCraftResults();
    }


    private void GetBluePrint()
    {
        bPList.Clear();
        foreach (var b in craftingCategory.blueprints)
        {
            if (b != null)
            {
                var rI = b.requiredItems;

                var listReqItems = new List<RequiredItem>();
                foreach (var e in rI)
                {
                    listReqItems.Add(new RequiredItem(e.item.ID, e.amount));
                }

                bPList.Add(new BlueprintList(b.name, b, listReqItems));
            }
        }
    }

    private void UpdateDirectory()
    {
        currentItemsInColl.Clear();

        foreach(var item in itemCollection)
        {
            if (item.item != null)
            {
                var cT = item.item.ID;
                if (currentItemsInColl.Find(u =>u.itemID == cT) == null)
                {
                    var itemCount = itemCollection.GetItemCount(item.item.ID);
                    currentItemsInColl.Add(new CurrentItemInCollList(item.item.ID, itemCount));
                }
            }
        }
    }

在这个方法中,我尝试在集合中找到相同的项目:

    private void UpdateFindMatchItems()
    {
        readyBlueprint = null;
        bool matchFailed = false;
        int requiredItemCount = 0;
        int currentItemsInCollCount = currentItemsInColl.Count;

        foreach(var bp in bPList)
        {

            requiredItemCount = bp.RequiredItems.Count;
            foreach(var rI in bp.RequiredItems)
            {

               if(CompareLists(currentItemsInColl, bp.RequiredItems))
                {
                    print("aa");
                }

            print(currentItemsInCollCount);
        }
    }

    private void UpdateCraftResults()
    {
        rewardCollection.Clear();
        if (readyBlueprint != null)
        {
            foreach (var items in readyBlueprint.resultItems)
            {
                rewardCollection.AddItem(items.item,null,true,false);
            }
        }
    }

我尝试这样的事情但是不会使用这个列表:

    public static bool CompareLists<T>(List<T> aListA, List<T> aListB)
    {
        if (aListA == null || aListB == null || aListA.Count != aListB.Count)
            return false;
        if (aListA.Count == 0)
            return true;
        Dictionary<T,T> lookUp = new Dictionary<T,T>();
        // create index for the first list
        for (int i = 0; i < aListA.Count; i++)
        {
            uint count = 0;
            if (!lookUp.TryGetValue(aListA[i], out count))
            {
                lookUp.Add(aListA[i], 1);
                continue;
            }
            lookUp[aListA[i]] = count + 1;
        }
        for (int i = 0; i < aListB.Count; i++)
        {
            uint count = 0;
            if (!lookUp.TryGetValue(aListB[i], out count))
            {
                // early exit as the current value in B doesn't exist in the lookUp (and not in ListA)
                return false;
            }
            count--;
            if (count <= 0)
                lookUp.Remove(aListB[i]);
            else
                lookUp[aListB[i]] = count;
        }
        // if there are remaining elements in the lookUp, that means ListA contains elements that do not exist in ListB
        return lookUp.Count == 0;
    }
}

这是我的名单:

/*     LISTS     */


[Serializable]
public class CurrentItemInCollList
{
    public uint itemID;
    public uint itemAmount;

    public CurrentItemInCollList(uint newitemID, uint newItemAmount)
    {
        itemID = newitemID;
        itemAmount = newItemAmount;
    }
}

[Serializable]
public class BlueprintList
{
    public string bluePrintName;
    public CraftingBlueprint craftingBlueprint;
    public List<RequiredItem> RequiredItems = new List<RequiredItem>();

    public BlueprintList(string newBluePrintName, CraftingBlueprint newcraftingBlueprint, List<RequiredItem> list)
    {
        bluePrintName = newBluePrintName;
        craftingBlueprint = newcraftingBlueprint;
        RequiredItems = list;

    }
}
[Serializable]
public class RequiredItem
{
    public uint itemID;
    public uint itemAmount;

    public RequiredItem( uint newitemID, uint newItemAmount)
    {
        itemID = newitemID;
        itemAmount = newItemAmount;
    }
}

我忘了.. CurrentItemInCollList.itemAmount可以是&gt; = RequiredItems.itemAmount

2 个答案:

答案 0 :(得分:0)

Dictionary使用哈希值来比较对象。 存储的类必须实现public override int GetHashCode(){}

答案 1 :(得分:0)

使用Linq - 这是一个小型控制台示例:

class Program
{
    static void Main(string[] args)
    {
        //Required list
        List<Order> currentItemsInColl = new List<Order>();
        currentItemsInColl.Add(new Order() { Name = "bike1", Id = "01" });
        currentItemsInColl.Add(new Order() { Name = "bike4", Id = "04" });

        //List of all items
        List<BPP> bPList = new List<BPP>();
        bPList.Add(new BPP() { BikeName = "bike1", Idzzz = "01" });
        bPList.Add(new BPP() { BikeName = "bike2", Idzzz = "02" });
        bPList.Add(new BPP() { BikeName = "bike3", Idzzz = "03" });
        bPList.Add(new BPP() { BikeName = "bike4", Idzzz = "04" });
        bPList.Add(new BPP() { BikeName = "bike5", Idzzz = "05" });

        //Blueprint List
        List<BPP> Blueprint = new List<BPP>();

        //get all items into the Blueprint list
        foreach (Order i in currentItemsInColl)
        {
            List<BPP> tmp = bPList.FindAll(x => x.Idzzz.Contains(i.Id));
            //here you add them all to a list
            foreach (BPP item in tmp)
            {
                Blueprint.Add(item);
            }
        }

        Console.ReadLine();
    }

}
public class Order
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class BPP
{
    public string Idzzz { get; set; }
    public string BikeName { get; set; }
}

旁注:我正在比较每个列表中的ID!希望它有所帮助。