2017-05-05 53 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]; 
} 

也不是最终的解决办法,但应该给你一些想法。

相关问题