Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Barruchi · Jan 19, 2017 at 12:10 AM · listdatabaselinq

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)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image juicyz · Jan 19, 2017 at 12:30 AM 0
Share

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

avatar image Barruchi · Jan 19, 2017 at 03:18 AM 0
Share

@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.

avatar image juicyz Barruchi · Jan 19, 2017 at 04:13 AM 0
Share

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?

avatar image James2Games Barruchi · Jan 19, 2017 at 04:56 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

http://pastebin.com/BQyKwy6C

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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: alt text


2017-01-25.png (32.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges