- Home /
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.
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;
}
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?
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;
}
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;
}
That's what Bunny83 has covered in his comment above. :-)
I just overlooked "counterpart" word from the question, I guess.
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.
Your answer
Follow this Question
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