2017-05-05 51 views
-1

我試圖將所有11個數組合併成一個大數組。所有數組都有相同數量的元素,每個數組中的所有元素都對應於其他數組中的元素。例如,Days數組的第一個元素對應於Depths數組的第一個元素,IRIS_IDs數組的第一個元素,Latitudes數組的第一個元素等等。如何將多個數組合併成一個?

當顯示控制檯屏幕上的合併數組就應該是這個樣子: enter image description here

我讀的數據到每個陣列從包含相應的數據

*編輯單獨的文本文件 - 我需要使它能夠搜索與特定月份相對應的所有數據。例如,如果我選擇在一月份輸入,控制檯將顯示與1月相對應的所有數據。因此在這個例子中將會顯示1月份發生的所有關於地震的數據。

+1

看看'郵編' –

+0

對於顯示你不需要合併數組。你需要數組還是隻顯示特定格式的數據? – BWA

+0

你不想將數組「合併」爲一個,你想從數組中創建一個複雜的結構。 – Gusman

回答

0

將示例簡化爲2個數組。

事情是這樣的:

string[] Days = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt"); 
string[] Depths = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt"); 
... 

string[] newArray = new string[Days.Length]; 
for(int x = 0; x < Days.Length;x++) 
{ 
    newArray[x] = string.Format("{0} {1}", Days[x], Depths[x]); 
} 
0

我會用新的數據對象的列表解決這個問題。你會想循環並在每個數組中使用相同的迭代器創建一個新對象。

首先創建新對象:

public class MyDataObject 
{ 
    public string Days { get; set; } 
    public string Depth { get; set; } 
    public string IRIS_IDs { get; set; } 
    public string Latitudes { get; set; } 
    // etc ... 
} 

然後設置你的函數,它的循環:

public IEnumerable<MyDataObject> MyObjectBuilder() 
{ 
    // Declare return object 
    var result = new List<MyDataObject>(); 

    // Get your data 
    string[] Days = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt"); 
    string[] Depths = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt"); 
    string[] IRIS_IDs = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\IRIS_ID_1.txt"); 
    string[] Latitudes = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Latitude_1.txt"); 
    // etc ... 

    // Loop through items to build objects 
    for(var i = 0; i <= Days.length(); i++) 
    { 
     result.Add(new MyDataObject { 
      Days = Days[i], 
      Depths = Depths[i], 
      IRIS_IDs = IRIS_IDs[i], 
      Latitudes = Latitudes[i], 
      // etc ... 
     } 
    } 

    // Return result 
    return result; 
} 
1

如果你需要的僅僅是壓縮,所以你可以打印數據,你可以也:

var maxItems = (new int[] { Days.Length, Depths.Length, IRIS_IDs.Length,Latitudes.Length }) 
       .Max(); 

var result = Enumerable.Range(0, items) 
      .Select(i => string.Join("\t", new [] { Days.ElementAtOrDefault(i), 
               Depths.ElementAtOrDefault(i), 
               IRIS_IDs.ElementAtOrDefault(i), 
               Latitudes.ElementAtOrDefault(i) })); 

好像你はNT才能然後在收集進行操作,以便而不只是串連創建自定義對象來正確的數據:

public class Data 
{ 
    public string Day { get; set; } 
    public string Depth { get; set; } 
    public string IRIS_ID { get; set; } 
    public string Latitude { get; set; } 

    public override string ToString() 
    { 
     return $"{Day}, {Depth}, {IRIS_ID}, {Latitude}"; 
    } 
} 

然後你可以:

var maxItems = (new int[] { Days.Length, Depths.Length, IRIS_IDs.Length,Latitudes.Length }) 
       .Max(); 

var reuslt = Enumerable.Range(0, maxItems) 
      .Select(i => new Data 
      { 
       Day = Days.ElementAtOrDefault(i), 
       Depth = Depths.ElementAtOrDefault(i), 
       IRIS_ID = IRIS_IDs.ElementAtOrDefault(i), 
       Latitude = Latitudes.ElementAtOrDefault(i) 
      }).ToList(); 

此實現將工作「盡最大努力」填充所有對象的所有數據 - 因此,如果在某些文件中缺少記錄,將在相應對象中包含null

+0

@Illimar - 這是否幫助你解決問題? –

1

您可以創建一個類來聚合想要顯示的所有字段,然後遍歷所有元素,併爲每個迭代創建此類的新實例並添加到列表中。喜歡的東西:

Class Merge 
{ 
    public int Days {get; set;} 
    public int Depths {get; set;} 
etc... 
} 

for (int i = 0; i < Days; i++) 
{ 
    var merge = new Merge(){Days = Days[0], Depths = Depths[0], ...} 

    mergedList.Add(merge); 
} 
0

你可以做到這一點,以及

string[] Days = System.IO.File.ReadAllLines(@"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt"); 
     string[] Depths = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt"); 
     string[] IRIS_IDs = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\IRIS_ID_1.txt"); 
     string[] Latitudes = System.IO.File.ReadAllLines(@"C: \Users\Illimar\Desktop\Algorithms and Comlexity2\Latitude_1.txt"); 

     List<string> result = new List<string>(); 
     (new[] { Days, Depths, IRIS_IDs, Latitudes }).ToList().ForEach(a => result.AddRange(a)); 
0

這裏有兩種方式來完成你想要的東西。第一種解決方案是使用數組,如你所問,另一種使用Dictionary。

在任一情況下,具有一個枚舉定義數據的文件類型:

enum DataFileType 
{ 
    Days = 0, 
    Depths, 
    IRIS_IDs, 
    Latitudes, 
    Longitudes, 
    Magnitudes, 
    Months, 
    Regions, 
    Times, 
    Timestamps, 
    Years 
} 

對於陣列解決方案,我們將使用DATAFILETYPE定義的文件路徑的陣列和創建數據的平行陣列:

static readonly string[] FileSpecs = new string[] 
{ 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\IRIS_ID_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Latitude_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Longitude_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Magnitude_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Month_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Region_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Time_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Timestamp_1.txt" , 
    @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Year_1.txt" 
}; 

static void Main(string[] args) 
{ 
    string[][] data = new string[FileSpecs.Length][]; 
    // read the data 
    for (int i = (int)DataFileType.Days; i <= (int)DataFileType.Years; i++) 
     data[i] = System.IO.File.ReadAllLines(FileSpecs[i]); 

    // grab some data 
    string[] IRIS_IDs = data[(int)DataFileType.IRIS_IDs]; 
} 

這個陣列解決方案很好 - 但不是很靈活,並且將DataFileType轉換爲int是單調乏味的

使用字典提供了更大的靈活性:

static readonly Dictionary<DataFileType, string> FileMap = new Dictionary<DataFileType, string> { 
    { DataFileType.Days, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt" }, 
    { DataFileType.Depths, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt" }, 
    { DataFileType.IRIS_IDs, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\IRIS_ID_1.txt" }, 
    { DataFileType.Latitudes, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Latitude_1.txt" }, 
    { DataFileType.Longitudes, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Longitude_1.txt" }, 
    { DataFileType.Magnitudes, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Magnitude_1.txt" }, 
    { DataFileType.Months, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Month_1.txt" }, 
    { DataFileType.Regions, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Region_1.txt" }, 
    { DataFileType.Times, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Time_1.txt" }, 
    { DataFileType.Timestamps, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Timestamp_1.txt" }, 
    { DataFileType.Years, @"C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Year_1.txt" } 
}; 

static void Main(string[] args) 
{ 
    // read data - map FileDataType to data file content 
    var dataMap = new Dictionary<DataFileType, string[]>(); 
    foreach (var kv in FileMap) 
     dataMap[kv.Key] = System.IO.File.ReadAllLines(kv.Value); 
    // grab some data 
    string[] data = dataMap[DataFileType.IRIS_IDs]; 
} 

也不是最終的解決辦法,但應該給你一些想法。

相關問題