0
void main() {
  List arr = [1, 2, 3, 4, 5];
  int sums = 0;
  List all = [];
  int length = arr.length;
  for (int i = 0; i < length; i++) {
    for (int j = 0; j < /**/ length - 1 /**/; j++) {
      if (arr[i] == arr[j]) {
        print('removed $i = ${arr[j]}');
        arr.removeAt(j);
      }
      print('$i = ${arr[j]}');
      sums += arr[j];
    }
    all.add(sums);
    sums = 0;
    arr.clear();
    arr = [1, 2, 3, 4, 5];
  }
  print(all);
}

上面的代码打印 [14, 13, 12, 11, 10]
但如果我使用arr.length-1它打印 [9, 8, 7, 11, 10]

void main() {
    List arr = [1, 2, 3, 4, 5];
    int sums = 0;
    List all = [];
    int length = arr.length;
    for (int i = 0; i < length; i++) {
    for (int j = 0; j < /**/ arr.length - 1 /**/; j++) {
        if (arr[i] == arr[j]) {
        print('removed $i = ${arr[j]}');
        arr.removeAt(j);
        }
        print('$i = ${arr[j]}');
        sums += arr[j];
    }
    all.add(sums);
    sums = 0;
    arr.clear();
    arr = [1, 2, 3, 4, 5];
    }
    print(all);
}
4

0 回答 0