【发布时间】:2020-07-21 15:13:05
【问题描述】:
使用 Dart,什么是遍历字符串列表以搜索特定字符串的好方法,当找到字符串时将其移动到列表的前面?
例如,如果要查找字母“b”。找到它,把它移到前面。
['a', 'b', 'c']
=> ['b', 'a', 'c']
对Dart不太了解,我用简单的for循环解决了
List<String> example = ['a', 'b', 'c'];
for (int i = 0; i< example.length; i++) {
if (example[i] == 'b') {
final temp = example[0];
example[0] = example[i];
example[i] = temp;
}
}
在 dart 中是否有一种方法可以调用列表上的某个数组方法来查找特定项目,将其从原始列表中删除并返回已删除的项目?就像 JS 中的.splice()。
【问题讨论】: