2016-11-28 95 views
0
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; 
    } 
} 

我想从临时列表中删除某些内容。为什么它不起作用?我没有看到它是如何修复的,在我解决的其他问题中,我使用了类似的方法,它工作(即使几乎相同)什么使得这是Dart中的一个固定长度列表?

请注意我对Dart有点新,所以请原谅我这种问题,但我找不出解决方案。

查找达特链接

Code in Dart

+0

请将代码作为文本直接添加到您的问题中,而不是链接的屏幕截图。 –

+0

你如何创建列表?如果你做'新列表(6)',它将变成一个固定长度的列表,有6个条目。 –

+0

checkLength(arrayToSingularElements(toColorBlockArray(input)))。join(''); –

回答

0

可用的代码可以确保tempOutput没有一个固定长度的列表中初始化它作为

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

从而宣告tempOutput到是一个可变的副本input

FYI它也看起来你有在你的程序中的另一个bug,因为你在你的for循环更新步骤做i + difference,但我想你想i += difference

+0

'var tempOutput = input.toList()'是相似的。 –

0

你可以试试这段代码,让我知道是那样吗?

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 = i + difference) { 
     tempOutput.removeAt(i); //Removing the value from the list 
     } 
    } 
    return tempOutput.toList(); //Return Updated list 
    } else { 
    return input.toList(); 
    } 
} 

注意:使用的“1 +差”,这是例如在第一次迭代中相同的值说你I = 1和差值= 1,则“tempOutput.removeAt(I)”将在删除值“ 1“的位置,再次在第二次迭代中,您尝试删除相同的位置,因此错误清楚地指出”无法从固定长度移除“

这里,i值必须为每个迭代过程递增或递减,在缺少的for循环中。

+0

是的,我刚才已经明白了这一点,并得到了和你一样的解决方案。 –

+0

感谢@LukeMuller,如果你觉得这个工作正常,那么你可以接受作为答案,这可能有助于未来。 –

0

@ harry-terkelsen的答案对解决定长问题非常有帮助。

对于那些询问我的算法的人: 不同之处在于想要删除一些字符时跳过字符的数量。此外,我不得不改变for循环,因为它没有做到我想要的。

修正在这里! https://github.com/luki/wordtocolor/blob/master/web/algorithms.dart

谢谢你理解我!

+0

哦,我刚刚意识到@BHUVANESH MOHANKUMAR得到了同样的问题解决方案。 –

+0

谢谢@Luke Muller –

相关问题