Search multiples INT in a custom class List

Hello Everyone!

Well I have a list (2k entries) with this custom class:

    public class ItemClass{
    	public int Item;
    	public string Date;
    	public int n1,n2,n3,n4,n5,n6;
    }

And an array with 3 entries.

public class TestNumbers {
	private List<ItemClass> List;

	private int[] numbers = new int[3]{12,24,32};
}

How I can find away to search if some item of this list have these 3 entries?

(I already done that with a weird function using loop, but is too slow)

instead of List you can use Dictionary and then you can use the ContainsKey method to do whatever you are trying to do with that Loop

Here! A friend of my had write this code. And Worked!

  • “Instead of using || I changed for &&”
  • And that thing with the var Random rnd is just something to calculate the timing

This will take up a bit of memory but will be exponentially faster than searching a list.
Slight modification where we no longer have the Item field in ItemClass as we don’t need an index, and I changed the n1, n2, n3 into an array as all n’s have the same properties.

public class Program
{
    public class ItemClass
    {
        public string Date;
        public int[] n;
    }

    private Dictionary<int, List<ItemClass>> items = new Dictionary<int, List<ItemClass>>();

    public bool NumberHasBeenUsed(int value)
    {
        return items.ContainsKey(value);
    }

    public ItemClass CreateNewItem(int[] numbers)
    {
        ItemClass item = new ItemClass();
        item.n = numbers;
        for (int i = 0; i < numbers.Length; i++)
        {
            var value = numbers*;*

List list = null;
if (NumberHasBeenUsed(value))
{
list = items[value];
}
else {
list = new List();
items.Add(value, list);
}
list.Add(item);
}

return item;
}

public void Main()
{
List allitems = new List(2000);
for (int i = 0; i < 2000; i++)
{
int[] n = new int[] { Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100) };
ItemClass item = CreateNewItem(n);
allitems.Add(item);
}

foreach (KeyValuePair<int, List> pair in items)
{
Debug.Log("We have " + pair.Value.Count + “x of the number '” + pair.Key + “'”);
}
}
}
Output:
[86645-2017-01-25.png|86645]*
*