- Home /
Checking a list containing another list, and checking if the elements in the list are equal problem
So it is a bit hard to explain but I am reading through a list containing another list. So lets say I have this
FirstList[0].SecondList[0] = 0;
FirstList[0].SecondList[1] = 0;
FirstList[1].SecondList[0] = 1;
FirstList[1].SecondList[1] = -1;
So basically I want to be able to compare:
FirstList[0].SecondList[0] = 0;
FirstList[0].SecondList[1] = 0;
By themselves and then after that compare:
FirstList[1].SecondList[0] = 1;
FirstList[1].SecondList[1] = -1;
by themselves. I have been going through it all night and have come up with nothing. All I want is a push so I can find the answer. Thank you in advance for any reply. Cheers!
Answer by Glurth · Apr 01, 2018 at 05:49 PM
The items that you want to compare are lists of intergers, regardless of where in your FirtList, they are located. I would first generate a simple CompareIntLists function, that takes two lists as a parameter. e.g. (untested/uncompiled)
bool CompareIntLists (List<int> a, List<int> b)// warning: assumes the lists are in the same order!
{
if(a.Count!=b.Count) return false;
for(int i=0;i<a.Count;i++) if(a[i]!=b[i]) return false
return true;
}
Now, you can simply pass whichever lists you want compared to this function.. e.g.
bool compareResult0001=CompareIntLists(FirstList[0].SecondList[0],FirstList[0].SecondList[1]);
bool compareResult1011=CompareIntLists(FirstList[1].SecondList[0],FirstList[1].SecondList[1]);
Though I suspect, you may wish to implement a nested-loop, rather than hard-coding which element indices to compare.
e.g.
for(int i=0;i<FirsList.Count;i++)
{
for(int j=0;j<FirstList[i].SecondList.Count;j++)
{
for(int k=0;k<FirstList[i].SecondList.Count;k++)//note: both these inner-loops use "i" as the index for FirstList. To compae against OTHER firstList elements, you will need to nest this stuff inside yet ANOTHER loop.
{
if(k!=j) //dont compare against itself
if(!CompareIntLists(FirstList[i].SecondList[j],FirstList[i].SecondList[k]))
return false
}
}
}
return true;