問題描述
我在解析文本文件時構建了兩個數組.第一個包含列名,第二個包含當前行的值.我需要一次遍歷兩個列表來構建地圖.現在我有以下內容:
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
}
這篇關于如何一次迭代兩個數組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!