Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 skylem · Mar 23, 2015 at 01:01 PM · listlistscompareequal

bool to true from a list comparison.

I have 2 int lists which i need to compare the values and see if they are all equal to one another if they are then i want to switch a bool to true otherwise i want that bool false.

Would anyone know the correct code to do this?

Elaboration -- i am using the 2 int lists to see if i have met costs for building resources one list, currentResources and the other, requiredResources if all values in currentResources are equal to there counterparts within requiredResources i want my bool buildingCompleted to be true if any one of these values is below there counterpart i want buildingCompleted to be false. the way these are compared would be the same as their order so requiredResources[0] is compared to currentResources[0], tried searching but i can't seem to word the question in a way that i find answers. so if anyones has or knows where i can find one i'd be very greatful.

Comment
Add comment
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

1 Reply

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

Answer by HarshadK · Mar 23, 2015 at 01:15 PM

Considering you have two lists:

 List<int> currentResources = new List<int>();
 List<int> requiredResources = new List<int>();

Now you can call the following method to compare two lists for equality

 bool areTheySame = AreListsSame(currentResources, requiredResources);

The method:

 bool AreListsSame(List<int> list1, List<int>list2)
 {
     // We start with the assumption that both the lists are same
     bool areSame = true;
     
     for(int i = 0; i < list1.Count; i++)
     {
         // If list2 does not contain any of the item from list1 then we set areSame to be false and return
         if(!list2.Contains(list1[i]))
         {
             areSame = false;
             return areSame;
         }
     }
     return areSame;
 }
Comment
Add comment · Show 6 · 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 skylem · Mar 23, 2015 at 01:27 PM 0
Share

Thanks alot for that i had no idea it was as simple as that. edit-- It works but it has a small problem if the last resource in the lists are equal to then the bool is true regardless of whether the previous values are equal or not do u know of a way i could fix this?

avatar image Bunny83 · Mar 23, 2015 at 01:53 PM 0
Share

Using List.Contains doesn't make much sense in this case. just compare each list item with the other:

 bool AreListsSame(List<int> list1, List<int>list2)
 {
     // if the lists have different item counts, they can't be equal.
     if (list1.Count != list2.Count)
         return false;
     for(int i = 0; i < list1.Count; i++)
     {
         // If one item in the list doesn't match the same item in the other list, just return false
         if(list1[i] != list2[i])
             return false;
     }
     // We only get here when all items have matched.
     return true;
 }
avatar image Bonfire-Boy · Mar 23, 2015 at 01:58 PM 0
Share

Careful now! That's not a sameness test it's an inclusion test.

It will return true iff list2 contains a copy of everything in list1, regardless of whether or not list2 contains other entries (including duplicates), and regardless of whether or not list1 contains duplicates (so if list1 contains two zeros, list2 only has to contain one zero for the test to succeed).

It's also not an answer to the question in that the question is looking for a more thorough kind of "list-sameness" where the elements are in the same place.

I would first check that the lists' lengths are the same, and then just loop from 0 to one less than that length, comparing list1[i] with list2[i].

 bool AreListsSame(List<int> list1, List<int>list2)
  {
      if ( list1.Count != list2.Count ) return false;
 
      for(int i = 0; i < list1.Count; i++)
      {
          if ( list2[i] != list1[i] ) return false;
      }
      return true;
  }
avatar image HarshadK · Mar 23, 2015 at 02:05 PM 0
Share

That's what Bunny83 has covered in his comment above. :-)

I just overlooked "counterpart" word from the question, I guess.

avatar image skylem · Mar 23, 2015 at 02:10 PM 0
Share

Thanks Bonfire Boy, that one did it for me simple fix in retrospect especially when i consider how long i spent trying to get other methods working.

Show more comments

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

21 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Comparing two lists to find the difference 1 Answer

List in prefab instantiated object not saving after Awake 0 Answers

Make Lists within a List 0 Answers

Help: items not getting added to list. 1 Answer


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