如何将多个数组合并为一个? [英] How can I merge multiple Arrays into one?

查看:111
本文介绍了如何将多个数组合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有11个数组合并为一个大数组.所有数组都具有相同数量的元素,并且每个数组中的所有元素都与其他数组中的元素相对应.例如,Days数组的第一个元素对应于Depths数组的第一个元素,对应于IRIS_IDs数组的第一个元素,对应于Latitudes数组的第一个元素,依此类推.

I'm trying to merge all of the 11 arrays into one big array. 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.

在控制台屏幕上显示合并的阵列时,它应如下所示:

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

然后您可以:

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

这篇关于如何将多个数组合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆