foreach读取多个数组

时间:2016-04-15 05:01:04

标签: c# arrays foreach

当我运行时,我只获取Month.txt文件的内容,我明白这只是因为我已将其设置为foreach中的字符串,但是我无法弄清楚我是怎么去的关于将其他文件添加到此以及我是否获得所有文件的内容而不仅仅是月份?

string[] month = System.IO.File.ReadAllLines
            (@"E:\project1\input\Month.txt");

            string[] 1_AF = System.IO.File.ReadAllLines
            (@"E:\project1\input\1_AF.txt");

            string[] 1_Rain = System.IO.File.ReadAllLines
            (@"E:\project1\input\1_Rain.txt");

            string[] 1_Sun = System.IO.File.ReadAllLines
            (@"E:\project1\input\1_Sun.txt");

            string[] 1_TBig = System.IO.File.ReadAllLines
            (@"E:\project1\input\1_TBig.txt");

            string[] 1_TSmall = System.IO.File.ReadAllLines
            (@"E:\project1\input\1_TSmall.txt");

            System.Console.WriteLine("Contents of all files =:");
            foreach (string months in month)
            {
                Console.WriteLine(months + "\t" + 1_AF + "\t" + 1_Rain + "\t" + 1_Sun + "\t" + 1_TBig + "\t" + 1_TSmall);
            }
            Console.ReadKey(); 

3 个答案:

答案 0 :(得分:2)

SELECT count(duration) from record group by user_id, aaa_id循环提供给定集合的迭代器。如果您需要多个集合的数据,则需要多个迭代器。

如果所有数组的大小相同,则可以始终使用传统的 foreach循环并使用数字索引访问数组位置:

for

答案 1 :(得分:0)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace SOFAcrobatics
{
    public static class Launcher
    {
        public static void Main (String [] paths)
        {
            List<String> pool = new List<String>();
            foreach (String path in paths)
            {
                pool.AddRange(File.ReadAllLines(path));
            }
            // pool is now populated with all the lines of the files given from paths
        }
    }
}

答案 2 :(得分:0)

即使您有不同大小的数组,以下解决方案也可以使用。如果所有数组的大小相同,则可以始终使用单个迭代器。

static void Main(string[] args)
    {
        string[] a = System.IO.File.ReadAllLines
        (@"E:\test\a.txt");

        string[] b = System.IO.File.ReadAllLines
        (@"E:\test\b.txt");

        string[] c= System.IO.File.ReadAllLines
        (@"E:\test\c.txt");

        System.Console.WriteLine("Contents of all files =:");
        for (int x = 0, y = 0, z = 0; x < a.Length || y < b.Length || z < c.Length; x++,y++,z++)
        {
            string first = string.Empty, second = string.Empty, third = string.Empty;

            if (x < a.Length)
                first = a[x];
            if (y < b.Length)
                second = b[y];
            if (z < c.Length)
                third = c[z];

            Console.WriteLine("\t" + first + "\t" + second + "\t" + third);
        }
        Console.ReadKey();
    }