简体   繁体   中英

Dart While Loop usage

i have a logic mistake but i cant figure out, loop not stops when num= returnitemcount?

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    int num = 0;
    while (num < returnitemcount) {
      for (var i = 0; i < listfrom.length; i++) {
        if (!exceptions.contains(listfrom[i])) {
          returnlist.add(listfrom[i]);
          num++;
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist);
  // expected => 1,5,6
  // result => 1, 5, 6, 7, 8, 9
}

Your problem is that your while are only evaluated twice. The first time num is 0 and the second time it is 6 . It is not evaluated every time num is updated.

while loops does not mean that the code inside the block are running as long some statement are true. It means the code will loop as long the statement are true.

So what you need to do is to check num every time you update the value. Something like this:

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    int num = 0;
    while (num < returnitemcount) {
      for (var i = 0; i < listfrom.length; i++) {
        if (!exceptions.contains(listfrom[i])) {
          returnlist.add(listfrom[i]);
          if (++num >= returnitemcount) {
            break;
          }
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

I am not sure what you code are going to do exactly but you should be able to skip the while loop:

void main() {
  List<int> listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  List<int> exceptions = [2, 3, 4];
  List<int> exceptedlist(
      int returnitemcount, List<int> exceptions, List<int> listfrom) {
    List<int> returnlist = [];
    for (var i = 0; i < listfrom.length; i++) {
      if (!exceptions.contains(listfrom[i])) {
        returnlist.add(listfrom[i]);
        if (returnlist.length >= returnitemcount) {
          break;
        }
      }
    }
    return returnlist;
  }

  List<int> mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

And can properly even be shorten into:

void main() {
  final listAll = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  final exceptions = [2, 3, 4];

  final mylist = exceptedlist(3, exceptions, listAll);
  print(mylist); // [1, 5, 6]
}

List<int> exceptedlist(
        int returnitemcount, List<int> exceptions, List<int> listfrom) =>
    listfrom
        .where((element) => !exceptions.contains(element))
        .take(returnitemcount)
        .toList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM