I am asking, because I currently have something like this:
public class FoodList : MonoBehaviour
{
public MyClass[] food;
}
[System.Serializable]
public class MyClass
{
public MyEnum name;
public string description;
public float cost;
public int rating;
}
public enum MyEnum
{
Pizza,
Pie,
Pasta,
//....
}
So if my food array is fairly big, using this search method may be inefficient:
public MyClass FindFood(MyEnum name)
{
foreach (MyClass m_food in food)
{
if (name == m_food.name) return m_food;
}
return null;
}
Would it be more efficient to add a Dictionary to my FoodList class and search through it?
private Dictionary<MyEnum, MyClass> searchList = new Dictionary<MyEnum, MyClass>();
//.......
public MyClass GetFood(MyEnum name)
{
if (searchList.Contains(name))
{
return searchList[name];
}
else
{
MyClass m_food = FindFood(name);
searchList.Add(name, m_food);
return m_food;
}
}
Or do Dictionaries internally work like my “FindFood” method and it wouldn’t make a difference?
And are there any better ways to search through big arrays? I can think of the “How to look for a lion in a desert” method… But if Dictionaries are not working like my “FindFood” method, then I’d guess that they’d be more efficient… Of course I can also always use MyEnum’s integer value in order to search for a value within an array ![]()
But which way is the best?