2016-04-15 5 views
0

Month.txtファイルの内容のみを実行すると、これはforeachの文字列として設定されているためですが、他の文字を追加する方法についてはうまくいかないこのファイルも同様に、月だけでなくすべてのファイルの内容を取得できますか?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(); 
+0

あなたは、あなたは、配列 –

+0

あなたが 'List'、' Dictionary'、または 'Tuple'を使用して考えがありますのためのforeachを適用することができ、配列のリストを作成し、そのリストに個々のファイルのコンテンツを追加する必要がありますか?彼らはあなたのような場合に非常に便利です。 – Ian

答えて

2

ループforeachループは、指定されたコレクションのイテレータを提供します。複数のコレクションのデータが必要な場合は、複数のイテレータが必要になります。

すべての配列が同じサイズのものであるならば、あなたは常に伝統的なforループを使用することができますし、アレイ位置にアクセスするための数値インデックスを使用します。

for (int i = 0; i < month.length; i++) 
{ 
    Console.WriteLine(month[i]+ "\t" + 1_AF[i] + "\t" + 1_Rain[i] + "\t" + 1_Sun[i] + "\t" + 1_TBig[i] + "\t" + 1_TSmall[i]); 
} 
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 
     } 
    } 
} 
0

以下のソリューションが動作しますあなたが異なるサイズの配列を持っていても。すべての配列のサイズが同じ場合は、常に1つのイテレータを使用できます。

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(); 
    } 
+0

'third = c [y];'ここで 'z'は正しくありませんか? –

+0

@FeDe、はい、あなたは正しいです、今修正されました。 –

関連する問題