Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-in loops order in dart

If I use for-in loop in dart on some List, are the iterations guaranteed to be in the correct order? e.g. Does the following code

List bars = getSomeListOfBars();

for(bar in bars){
    foo(bar);
}

in the exact same way as the following?

List bars = getSomeListOfBars();

for(int i=0;i<bars.length;i++){
    foo(bar[i]);
}

I have not found dart specific explanation anywhere, thank you.

like image 783
Lubomír Grund Avatar asked Jul 24 '16 09:07

Lubomír Grund


1 Answers

If you use for(x in y) with a collection that guarantees the iteration order, then for(x in y) is guaranteed to iterate in this order. If the collection itself doesn't guarantee the order, then of course for(x in y) doesn't as well.

like image 66
Günter Zöchbauer Avatar answered Nov 15 '22 06:11

Günter Zöchbauer