- Home /
Comparing Ints in Two List
I have 2 list that both contain an int.I want to compare the int in List 1 to the int in List 2.And if they're the same then i want the texture2d in list 2 to be the texture 2d in list 1.Can anyone ple give me an example script of the concept i am looking for.Thanks
Decomposing your question into distinct line of actions would do half the work:
I have 2 list that both contain an int.
I want to compare the int in List 1 to the int in List 2.
And if they're the same then i want the texture2d in list 2 to be the texture 2d in list 1.
In more technical terms:
listA as List<Class>
listB as List<Class>
for(all items in listA){
if listA[index].int == listB[index].int
listA[index].texture == listB[index].texture
}
Sorry is this javascript,im not experienced in javascript.Thanks alot for answering anyway.
Answer by HarshadK · Sep 16, 2014 at 09:40 AM
If you know the value of integer before hand then you can use List.Contains() on both the lists to check if they both contain that integer.
If you want to check if both lists have any common integer then you can do it like:
This code is not tested, but you can use the logic.
int i=0;
while(i < List1.Count)
{
if(List2.Contains(List1[i]))
{
// There is a common item so perform the required action.
}
else
{
i++; // We increment the value of i to check for next element from List1.
}
}
Im sorry but my int is in the list so i can create more than 1 element with different int.
Then the provided code is the logic for that. It is the base logic on top of it you can tweak it to create all the effects you want.
List is just the name of your classes and variables and are not actual collections. You had us think that those are collections of type List.
This can be done as:
// Instantiating your two classes and create an object for each of them
List1 list1Object = new List1();
List2 list2Object = new List2();
// You can compare the variables from those classes using an == operator.
if(list1Object.list1 == list2Object.list2)
{
// Do whatever you want to do
}
Sorry this is what i mean.
[System.Serializable]
Public class List1
Public texture2d list1texture;
Public text list1text;
Public int list1int;
}
[System.Serializable]
Public class List2
Public texture2d list2texture;
Public text list2text;
Public int list2int;
}
I want it to compare list1int to list2int and if they both match then list2text and list2texture to be list1text and list1texture.
Logic is still as per comment above.
// Instantiating your two classes and create an object for each of them
List1 list1Object = new List1();
List2 list2Object = new List2();
// You can compare the variables from those classes using an == operator.
if(list1Object.list1int == list2Object.list2int)
{
// Assign the text and texture from List2 to List1.
list1Object.list1text = list2Object.list2text;
list1Object.list1texture = list2Object.list2texture;
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
using char to extract integer numbers from a list - [UnityScript] 1 Answer
Can I create a list with an int/float and a string? C# 2 Answers
Convert String to int 1 Answer
Set int to object from list? 0 Answers