traversing two lists simultaneously
-
suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks
-
suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks
Use a
for
instead of aforeach
:for (int i = 0; i < a.Length && i < b.Length; i++)
{
if (a[i] == b[i])
{
...
}
}"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks
Lists work just like arrays, so you can use either an enumerator (foreach) or you can use an index value:
for(int index = 0; index < someValue; ++index) { if (a\[index\] == b\[index\]) { // true } else { // false } }
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks
If the "lengths" of a and b are equal, you can maintain your own index:
int i = 0;
foreach ( int value in a ){
bool result = (value == b[i++]);
}Note the suffix (++) operator in this case.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks
You could also use the `Zip` extension method, which takes a lambda with two arguments, which is called with the corresponding elements. It yields a IEnumerable of the results of those lambda calls.
var a = {1,2,3,4}; var b = {2,3,4,5} var c = a.Zip(b, (aa, bb)=> aa \* bb).ToList(); // c = {2, 6, 12, 20}; // : var d = {…}; var e = {…}; if (d.Zip(e, (dd, ee)=> dd==ee).All()) { // tests if all elements are equals (same as a.SequenceEqual(b))
Truth, James