Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 获取循环中项的索引以与存储的索引匹配_Loops_Flutter_Dart - Fatal编程技术网

Loops 获取循环中项的索引以与存储的索引匹配

Loops 获取循环中项的索引以与存储的索引匹配,loops,flutter,dart,Loops,Flutter,Dart,我有一个场景,如果字符串列表中的字符串与int列表(基于字符串列表生成)中的索引匹配,我希望执行一个操作 下面是一些伪代码,试图阐明我试图实现的目标 List wordIndex=[1,3,5]; List wordList=['this','is','a','test','a']; //伪码 wordList.forEach(word)){ if(单词索引项与单词索引匹配){ 做点什么; }否则{ 其他的东西; } } 如果(单词索引项与单词索引匹配)我遇到了问题,如果我有任何想法,我将不胜

我有一个场景,如果字符串列表中的字符串与int列表(基于字符串列表生成)中的索引匹配,我希望执行一个操作

下面是一些伪代码,试图阐明我试图实现的目标

List wordIndex=[1,3,5];
List wordList=['this','is','a','test','a'];
//伪码
wordList.forEach(word)){
if(单词索引项与单词索引匹配){
做点什么;
}否则{
其他的东西;
}
}

如果(单词索引项与单词索引匹配)我遇到了问题,如果我有任何想法,我将不胜感激。

只需使用
for
而不是
forEach

List<int> wordIndex = [1,3,5];
List<String> wordList = ['this', 'is','a', 'test', 'a'];

//Pseudo code
for (int i = 0; i < wordList.length; i++) {
  if (wordIndex.contains(i)) {
    do something;
  } else {
    od something else;
 }
}
List wordIndex=[1,3,5];
List wordList=['this','is','a','test','a'];
//伪码
for(int i=0;i
如果我理解正确,您想知道单词索引是否包含在您的
wordIndex
列表中,也就是说,您想获得所有
wordList
项目,其索引存储在
wordIndex

这有两种方法:

使用 在本例中,我们只是简单地检查当前索引是否存在于
wordIndex
列表中

for(var index=0;index
循环浏览
wordIndex
如果您只是对匹配的项目感兴趣,那么这种方法更合理。
在这里,我们循环遍历索引列表,然后在
wordList
中获得匹配的元素。但是,您将无法对不匹配的项目执行操作:

for(wordIndex中的最终索引){
词尾=词表[索引];
//做点什么
}

有趣的是,您如此习惯于循环帮助器函数而忘记了基本功能。谢谢你。