我正在尝试将所有11个阵列合并为一个大阵列。所有数组都具有相同数量的元素,并且每个数组中的所有元素都对应于其他数组中的元素。例如,Days数组的第一个元素对应于Depths数组的第一个元素,IRIS_IDs数组的第一个元素,Latitudes数组的第一个元素,依此类推。
我正在从包含相应数据的单独文本文件中将数据读入每个数组
*编辑 - 我需要这样做,以便我能够搜索与特定月份相对应的所有数据。例如,如果我选择在1月份输入,则控制台将显示与1月相对应的所有数据。因此,在这个例子中,将显示1月发生的所有地震数据。
答案 0 :(得分: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) }));
但似乎您希望对集合执行操作,而不是仅仅连接创建自定义对象以正确包含数据:
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
个值
答案 1 :(得分: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);
}
答案 2 :(得分: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]);
}
答案 3 :(得分: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;
}
答案 4 :(得分: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));
答案 5 :(得分:0)
以下两种方法可以实现您的目标。第一个解决方案是使用数组,正如您所要求的那样,另一个解决方案是使用Dictionary&#39>。
在任何一种情况下,使用枚举:
定义数据文件类型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是乏味的。
使用Dictionary提供了更大的灵活性:
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];
}
两者都不是终极解决方案,但应该给你一些想法。