是什么让这个Dart中的固定长度列表?

Luk*_*ern 8 algorithm list dart

 List<String> checkLength(List<String> input) {
  if (input.length > 6) {
    var tempOutput = input;
    while (tempOutput.length > 6) {
      var difference = (tempOutput.length/6).round() + 1;
      for (int i = 0; i < tempOutput.length - 1; i + difference) {
        tempOutput.removeAt(i); //Removing the value from the list
      }
    }
    return tempOutput; //Return Updated list
  } else {
    return input;
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图从临时列表中删除一些东西.为什么不起作用?我没有看到它是如何修复的,在我解决的其他问题中,我使用了类似的方法并且它有效(甚至相同)

请注意我对Dart有点新意,所以请原谅我这样的问题,但我无法弄清楚解决方案.

查找Dart Link中提供的代码

Dart中的代码

Har*_*sen 16

您可以tempOutput通过初始化它来确保它不是固定长度列表

var tempOutput = new List<String>.from(input);

从而声称tempOutput是一个可变的副本input.

仅供参考,你的程序中还有另一个错误,因为你正在进行i + differencefor循环更新,但我认为你想要i += difference.

  • var tempOutput = input.toList()将会类似。 (2认同)