How to find the List item with class without a c # looping unit

advertisements

How to find element in List class without looping ?

for example ordinary look up using Looping :

for(int i = 0; i < inventoryx.Player.items.Count; i++) {
            if(inventoryx.Player.items[i].itemName == "Wood") {
                Debug.log("You Find The Wood");
            }
            else {
                Debug.Log("Can't Find The Wood");
            }
        }

items.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// Make Class Item

public class item {
    public string itemName;
    public int itemID;
    public string itemDesc;
    public string itemIcon;
    public GameObject itemModel;
    public int itemTime;
    public int hightprice;
    public int stdprice;
    public int itemStock;
    public int harvest;
    public RawTree rawTree;
    public ItemType itemType;
    public ItemProd itemProd;
    public ItemLocation itemLocation;
    public int Lvlunlock;
    private string baseName;
    public int itemExp;

    public enum ItemType {
        Raw,
        Admirable,
        Valuable
    }

    public enum RawTree {
        BigTree,
        SmallTree,
        Field,
        None
    }

    public enum ItemProd {
        Corps,
        Dairy,
        JuiceJamMaker,
        Kitchen,
        Bakery,
        CraftHouse,
        ChickenCoop,
        FishingSpotMountain,
        CowPasture,
        LunaMine,
        PigPen,
        FishingSpotLake,
        TropicalWood,
        SheepPasture,
        FishingSpotSea,
        Beebox,
        HomeIndustry
    }

    public enum ItemLocation {
        Home,
        Orchard
    }

    public item (string name, int ID, string desc, int harvestx, int time, int stdpricex, int hightpricex, int stock, int Lvlunlockx, RawTree RawTree, ItemType type, ItemProd prod, string folderx, ItemLocation location, int Exp) {
        itemName = name;
        itemID = ID;
        itemDesc = desc;
        harvest = harvestx;
        itemTime = time;
        stdprice = stdpricex;
        hightprice = hightpricex;
        itemStock = stock;
        Lvlunlock = Lvlunlockx;
        rawTree = RawTree;
        itemType = type;
        itemProd = prod;
        itemIcon = folderx;
        itemLocation = location;
        itemExp = Exp;
    }

    public item() {

    }
}

Is there any idea to find an items without looping such as above ? because using looping to find the elements data is take more memory to do it. If there is more than 100 items it will make it lag and take more time.

Thanks


Using LINQ you can write

using Sytem.Linq;
...

bool hasWood = inventoryx.Player.items.Any(i => i.ItemName == "Wood");

Or, to find the index:

int index = items.FindIndex(i => i.ItemName == "Wood");

Which returns the index of the first item matching the condition, or -1 to indicate no matches found.

This will still be O(n) though, just like your loop, but it is a bit more terse and readable.

Also, C# offers a foreach statement for iterating through IEnumerable collections, this helps prevent off-by-one exceptions:

foreach (var item in inventoryx.Player.items)
{
    if (item.ItemName == "Wood")
        Debug.log("You Find The Wood");
}