2017-10-04 1 views
0

Ich habe ein Problem. Ich versuche, zwei Liste currentItemsInColl und bPList zu vergleichen. Inside bPList Ich habe andere Liste RequiredItems und jetzt ist was ich brauche. Ich möchte vergleichen currentItemsInColl und RequiredItems und zurück bPList.craftingBlueprint. wissen Ich versuche zu vergleichen, aber ich nicht, wie es verwendet werden:/C# Vergleich zwei Listen und Rückgabewert

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)); 
       } 
      } 
     } 
    } 

In dieser Methode versuche ich gleichen Artikel in einer Sammlung finden:

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); 
      } 
     } 
    } 

ich somthing wie diese versuche, bin aber nicht funktioniert mit diesen Listen :

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; 
    } 
} 

Und das ist meine Listen:

/*  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; 
    } 
} 

Ich habe vergessen .. CurrentItemInCollList.itemAmount kann sein> = RequiredItems.itemAmount

+1

Haben Sie mit Linq versucht? 'listA.SequenceEquals (listB)' oder, falls nötig, fügen Sie zusätzlichen Parameter 'listA.SequenceEquals (listB, EqualityComparer)' –

+0

das ist guter Vorschlag, aber denken Sie daran, dass dann die Listen in der gleichen Reihenfolge sein müssen –

+0

Ich habe vergessen .. CurrentItemInCollList .itemAmount kann> = RequiredItems.itemAmount sein, damit die Liste nicht identisch sein kann. –

Antwort

0

Wörterbuch verwenden Hash-Werte, um Objekte zu vergleichen. Die gespeicherten Klassen public override int GetHashCode(){}

0

Verwendung Linq implementieren müssen - hier ist eine kleine Konsole Beispiel:

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; } 
} 

Nebenbei bemerkt: Ich bin der IDs in jeder der Listen zu vergleichen! Ich hoffe es hilft.

+0

Das ist nicht genau das, was ich will, aber ich habe eine Idee :) –