如何在一次遍历两个数组? [英] How to iterate over two arrays at once?

查看:572
本文介绍了如何在一次遍历两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在分析一个文本文件,内置两个数组。第一个包含了列名,第二个是包含从当前行的值。我需要两个列表遍历一次建立一个映射。现在,我有以下几点:

I have two arrays built while parsing a text file. The first contains the column names, the second contains the values from the current row. I need to iterate over both lists at once to build a map. Right now I have the following:

var currentValues = currentRow.Split(separatorChar);
var valueEnumerator = currentValues.GetEnumerator();

foreach (String column in columnList)
{
    valueEnumerator.MoveNext();
    valueMap.Add(column, (String)valueEnumerator.Current);
}

这工作得很好,但它并不完全符合我的优雅感,它变得非常毛茸茸的,如果阵列的数量大于2时(因为我有偶尔做)。有没有人有一个,更简洁成语?

This works just fine, but it doesn't quite satisfy my sense of elegance, and it gets really hairy if the number of arrays is larger than two (as I have to do occasionally). Does anyone have another, terser idiom?

推荐答案

如果有相同数量的列名,因为有每行中的元素, ?你能不能循环使用

if there are the same number of column names as there are elements in each row, could you not use a for loop?

var currentValues = currentRow.Split(separatorChar);

for(var i=0;i<columnList.Length;i++){
   // use i to index both (or all) arrays and build your map
}

这篇关于如何在一次遍历两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆