【问题标题】:C# Compare two list and return valueC#比较两个列表并返回值
【发布时间】:2017-10-04 12:03:55
【问题描述】:

我有问题。我尝试比较两个列表 currentItemsInCollbPList。在 bPList 我有其他列表 RequiredItems 现在是我需要的。 我想比较 currentItemsInCollRequiredItems 并返回 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 可以 >= RequiredItems.itemAmount

【问题讨论】:

  • 你试过 Linq 吗? listA.SequenceEquals(listB) 或者,如果需要,添加其他参数 listA.SequenceEquals(listB, EqualityComparer)
  • 这是个好建议,但请记住,列表的顺序必须相同
  • 我忘了.. CurrentItemInCollList.itemAmount 可以 >= RequiredItems.itemAmount 所以列表不能相同。

标签: c# list unity3d compare


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用 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!希望对您有所帮助。

    【讨论】:

    • 这不是我想要的,但我有想法:)
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2023-03-11
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多