简体   繁体   中英

How can I merge multiple Arrays into one?

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

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

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.

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.

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:

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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