- Home /
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)
So you have a list of ItemClass objects, and you want to search each ItemClass object if n1, n2, n3, n4, n5, and n6 contain the numbers in the numbers array?
Or you want to search the ItemClass list for their Item int to see if the numbers in the numbers array exist?
Other important questions.
Is there always 2k entries or is that dependent on something. Does the number of entries grow?
Is the 'int Item' value unique?
Are the n1, n2, n3, n4, n5, n6 values unique? What do these represent?
Are you constrained by memory space?
Define slow
@juicyz thanks for answer.
The first option.
Is a fixed number and grow by one each week.
int Item is the Index.
Seems like a lotery, every week, 6 new numbers.
It's a mobile app, so.. maybe?
On my pc the FOR make me wait for 15 secs. The problem would be on the mobile.
Can I see your old function? It seems that your best option is to maybe put this in a database or to use linq. How often does this searching for need to be done?
If n1, n2, n3, n4, n5, n6 are always a small number (between 0 and 100)
You could create a map from a number to an ItemClass object. Dictionary> nDict = new Dictionary>()
Then you can run a check against the dictionary to see if it had any given number in it as well as retrieve the ItemClass objects that contain that number as one of their (n1, n2, n3, n4, n5, n6) values.
However If you had 600 different numbers spread out over different ItemClasses this would use a lot of memory.
Answer by Barruchi · Jan 25, 2017 at 08:14 AM
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
Answer by aditya · Jan 19, 2017 at 05:03 AM
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
Answer by James2Games · Jan 25, 2017 at 09:07 AM
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[i];
List<ItemClass> list = null;
if (NumberHasBeenUsed(value))
{
list = items[value];
}
else {
list = new List<ItemClass>();
items.Add(value, list);
}
list.Add(item);
}
return item;
}
public void Main()
{
List<ItemClass> allitems = new List<ItemClass>(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<ItemClass>> pair in items)
{
Debug.Log("We have " + pair.Value.Count + "x of the number '" + pair.Key + "'");
}
}
}
Output:
Your answer
![](https://koobas.hobune.stream/wayback/20220612105118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Database Error (Sqlite) in unity 4.0 1 Answer
Problem with adding items to a list 1 Answer
C#, LINQ and Lists - help with 1 line of code? 1 Answer
List the button didn't works 0 Answers