简体   繁体   English

应该使用 List 的哪种方法将两个匹配的单词分配给 value

[英]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.

相关问题 如何比较两个动态列表以识别匹配和不匹配的记录,使用id(这将是唯一的) - How to compare Two List of dynamic to identify the matching and non matching records, using the id (which will be unique) flutter下面的设计应该使用哪个widget? - Which widget should be used for following design in flutter? CachedNetworkImage 和 CachedNetworkImageProvider 有什么区别? 应该分别用在什么情况下? - What is the difference between CachedNetworkImage and CachedNetworkImageProvider? Which case should each be used in? 以下线框的响应式 UI 应使用哪些小部件? - Which widgets should be used for responsive UI of the following wireframe? 在 null 上调用了方法“[]”。 均匀的价值应该在那里 - The method '[]' was called on null. even value should be there 如何更新用作 dart 中 map 值的列表 - how to update a list used as a value for a map in dart Null 检查运算符用于 null 值(列表) - Null check operator used on a null Value ( List ) NoSuchMethodError: Class '列表<dynamic> ' 没有匹配 arguments 的实例方法 'cast'</dynamic> - NoSuchMethodError: Class 'List<dynamic>' has no instance method 'cast' with matching arguments 两个匹配某个条件的列表中的多个对象如何求和 - How two sum multiple objects in a list matching a certain condition 将值分配给从 Flutter 中的 JSON 转换的列表 object 中的变量 - Assign value to a variable from list object converted from JSON in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM