- Home /
iterate through two lists element by element
I want to iterate through two lists, (listA and listB) element by element. listA element 0, then listB element 0, then ListA element 1, then listB element 1 etc. i tryed to achef this trough a nested for loop (see example). But this code starts with listA element 0, but then all ellements of` listB will pass. What do i have to change to iterate element by element?
for (int i = 0; i< listA.Count; i++)
{
placeHolderPosition = ListA[i];
Debug.Log("placeholderpositie" + ListA);
for (int t = 0; t<listB.Count; t++)
{
bookPosition = listB[t];
Debug.Log("boek" + listB);
}
}
//float dist = Vector3.Distance(placeHolderPosition, bookPosition);
//Debug.Log("Distance to other: " + dist);
}
first of all, you're iterating through an array
which is not the same as a List
second, are both arrays the same size? if so, then you can use the same iterator to check each element ins$$anonymous$$d of nesting the loops.
if they're not, then you'll need to iterate with the larger count and ignore elements in the smaller list that don't exist.
these are technicalities which may be unnecessary depending on what you're actually trying to do... from your questions, that's not clear so the above is the best advice given the lack of information.
thank you for your answer gjf. as you've probably noticed I'm not an experienced programmer. I have declared the lists as follows public List Lista = new List (); in the for loop I use Count ins$$anonymous$$d of Lenght. the lists are the same size. They both contain the positions of game objects. I want to compare them with Vector3.Distance
i should have spotted the listname.Count
item which shows that it's actually a List
(since you can access them like arrays).
if they're both the same size then all you need to do is something like
for (int i = 0; i < listSize.Count; i++)
{
if (listA[i].Distance == listB[i].Distance)
{
// do something
}
}
but the distance might not be exact so consider using $$anonymous$$athf.Approximately()
for the comparison.
and this (foolishly?) assumes that your List
contains the Vector3
's ;)
are ListA and ListB brought together in ListSize? Because ListSize does not exist in the current context.
Answer by JoshuaMcKenzie · Nov 23, 2015 at 09:10 PM
Just iterate as normal using the greater of the two list counts (if they don't match) and then compare only if the iterator continues to remain less than both list counts, breaking as soon as it fails to save on iterations.
EDIT: you can use Mathf.Min() instead of Max so that you don't need the if check and break
for(int i = 0; i<Mathf.Min(listA.Count,listB.Count);i++)
{
float dist = (listA[i] - listB[i]).magnitude;
//play with the dist here
}
Thank you, but It is slightly more complicated. Both lists contain elements of type Vector3 I want to check if these positions correspond. I think the approach you suggest this does not happen The function distance needs a beginning and an end point. The starting point is from ListA, the endpoint from ListB. If the distance between these objects is 0 then a know that there on the right place. Therefore, it is necessary to run through element after element from both lists.
i misunderstood slightly. if your lists contain Vector3's and you want to check the distance between them to be zero or close enough (i.e. they're in the same place), then you might want something like this:
if ($$anonymous$$athf.Approximately(listA[i], listB[i]))
{
// do something
}
i'm not near a machine to test so ymmv ;)
Can't you just do that with the dist
variable in your code? if (dist < some_threshold)...
and so on. Or am I misunderstanding beltmanmarc's last comment?
Tiny improvement, while I'm here - no need to use $$anonymous$$athf's float functions on ints... $$anonymous$$ath.$$anonymous$$in will do the job.
thank you Joshua$$anonymous$$c$$anonymous$$enzie it works
Your answer
![](https://koobas.hobune.stream/wayback/20220612050103im_/https://answers.unity.com/themes/thub/images/avi.jpg)