简体   繁体   English

如何将多个数组合并为一个?

[英]How can I merge multiple Arrays into one?

I'm trying to merge all of the 11 arrays into one big array. 我正在尝试将所有11个数组合并为一个大数组。 All of the arrays have same number of elements and all the elements in each array correspond to the elements in other arrays. 所有数组都具有相同数量的元素,并且每个数组中的所有元素都与其他数组中的元素相对应。 For example the first element of Days array corresponds to first element of Depths array, to first element of the IRIS_IDs array, to first element of the Latitudes array and so on. 例如,Days数组的第一个元素对应于Depths数组的第一个元素,对应于IRIS_IDs数组的第一个元素,对应于Latitudes数组的第一个元素,依此类推。

When the merged array is displayed on Console screen it should look something like this: 当合并的阵列显示在控制台屏幕上时,它应如下所示: 在此处输入图片说明

I am reading in the data into each array from separate text files that contain the corresponding data 我正在从包含相应数据的单独文本文件中将数据读入每个数组

*Edit - I need to make it so that I am able to search for all the data that corresponds to a specific month. *编辑-我需要进行编辑,以便能够搜索与特定月份相对应的所有数据。 For example if I choose to type in January, the Console will display all of the data that corresponds to January. 例如,如果我选择在1月键入,则控制台将显示与1月相对应的所有数据。 So in this example all the data about the earthquakes that happened in January will be shown. 因此,在此示例中,将显示有关一月份发生的地震的所有数据。

If all you need is just to zip so you can print the data you can also: 如果您只需要压缩即可打印数据,还可以:

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) }));

But it seems like you want to then perform operations on the collection so instead of just concatenations create a custom object to properly contain the data: 但是似乎您想对集合执行操作,因此创建串联的自定义对象以正确包含数据,而不仅仅是串联:

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}";
    }
}

And then you can: 然后您可以:

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();

This implementation will work by "best-effort" to populate all the data for all the objects - so if in some files you are missing records you will have null values in the corresponding objects 此实现将通过“尽力而为”来填充所有对象的所有数据-因此,如果在某些文件中缺少记录,则相应对象中的null

You can create a class to aggregate all the fields which you want to display, then iterate through all the elements and for each iteration create a new instance of this class and add to a list. 您可以创建一个类来聚合要显示的所有字段,然后遍历所有元素,并为每次迭代创建该类的新实例并添加到列表中。 Something like: 就像是:

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);
}

Example is simplified to 2 arrays. 示例简化为2个数组。

Something like this: 像这样:

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]);
}

I would solve this with a list of new data objects. 我将使用新数据对象列表来解决此问题。 You would want to loop through and create a new object with the same iterator in each array. 您可能想遍历并在每个数组中使用相同的迭代器创建一个新对象。

First create your new object: 首先创建您的新对象:

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 ...
}

Then set up your function that does the looping: 然后设置执行循环的函数:

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;
}

You can do this as well 您也可以这样做

 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));

Here are two ways to accomplish what you want. 这是完成所需内容的两种方法。 The first solution is with arrays, as you asked, and another with Dictionary's. 第一个解决方案是根据您的要求使用数组,而另一个解决方案是使用Dictionary的解决方案。

In either case, define your data file types with an enum: 无论哪种情况,都可以用枚举定义数据文件类型:

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

For the array solution we'll use DataFileType to define an array of file paths and create a parallel array of data: 对于数组解决方案,我们将使用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];
}

This array solution is fine - but not very flexible, and the casting of DataFileType to int is tedious . 这个数组解决方案很好-但不是很灵活,并且将DataFileType强制转换为int是乏味的

Using Dictionary provides a lot more flexibility: 使用字典提供了更多的灵活性:

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];
}

Neither are the ultimate solution but should give you some ideas. 两者都不是最终的解决方案,但应该给您一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM