[英]Which method of List should be used to assign two matching words to the value
1.//dart language in Flutter 1.//Flutter中的dart语言
2.//Which method of list should be used to assign two matching words to the value? 2.//应该用list的哪种方法给值赋值两个匹配的词?
3.//The duplicate value should appear next to its matching value? 3.//重复值应该出现在其匹配值旁边?
//Question: List colour=["peach","red","orange","yellow","white","pink","red","maroon"]; //问题:列表 colour=["peach","red","orange","yellow","white","pink","red","maroon"];
//Answer: //回答:
Print ["peach","red","red","orange","yellow","white","pink","maroon"]打印 ["peach","red","red","orange","yellow","white","pink","maroon"]
There is no 'silver bullet' in List, that is able to solve the task, but it possess several useful methods for the case. List 中没有能够解决任务的“灵丹妙药”,但它具有几种有用的方法。 There could be several ways to solve the problem.可能有几种方法可以解决问题。 For instance, I'm proposing the remove/insert way:例如,我提出删除/插入方式:
List<String> colour = ["peach","red","orange","yellow","white","pink","red","maroon", "pink", "red"];
for (int i = 0; i < colour.length; i++) {
int indexOfPossibleDuplication = colour.lastIndexOf(colour[i]);
if (indexOfPossibleDuplication > i) {
colour.removeAt(indexOfPossibleDuplication);
colour.insert(i, colour[i]);
}
}
print(colour); //[peach, red, red, red, orange, yellow, white, pink, pink, maroon]
I deliberately inserted several duplicates to your input example in order to show that the code do the trick in cases, when there is more than one duplication took place.我故意在您的输入示例中插入了几个重复项,以表明在发生多个重复项的情况下代码可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.