如何一次迭代两个数组?

How to iterate over two arrays at once?(如何一次迭代两个数组?)

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

问题描述

我在解析文本文件时构建了两个数组.第一个包含列名,第二个包含当前行的值.我需要一次遍历两个列表来构建地图.现在我有以下内容:

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);
}

这很好用,但它并不能完全满足我的优雅感,如果数组的数量大于两个(我偶尔必须这样做),它会变得非常毛茸茸.有没有人有另一个更简洁的成语?

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?

推荐答案

如果列名的个数和每一行的元素个数一样,可以不用for循环吗?

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
}

这篇关于如何一次迭代两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何一次迭代两个数组?

基础教程推荐