C# 如何将多个阵列合并为一个阵列?
我正在尝试将所有11个数组合并为一个大数组。所有数组都有相同数量的元素,每个数组中的所有元素都对应于其他数组中的元素。例如,Days数组的第一个元素对应于Deepth数组的第一个元素、IRIS_IDs数组的第一个元素、Latitudes数组的第一个元素等等 当合并阵列显示在控制台屏幕上时,它应该如下所示: 我从包含相应数据的单独文本文件中将数据读入每个数组C# 如何将多个阵列合并为一个阵列?,c#,arrays,merge,C#,Arrays,Merge,我正在尝试将所有11个数组合并为一个大数组。所有数组都有相同数量的元素,每个数组中的所有元素都对应于其他数组中的元素。例如,Days数组的第一个元素对应于Deepth数组的第一个元素、IRIS_IDs数组的第一个元素、Latitudes数组的第一个元素等等 当合并阵列显示在控制台屏幕上时,它应该如下所示: 我从包含相应数据的单独文本文件中将数据读入每个数组 *编辑-我需要进行编辑,以便能够搜索与特定月份对应的所有数据。例如,如果我选择输入一月,控制台将显示与一月对应的所有数据。因此,在本例中,
*编辑-我需要进行编辑,以便能够搜索与特定月份对应的所有数据。例如,如果我选择输入一月,控制台将显示与一月对应的所有数据。因此,在本例中,将显示1月份发生的地震的所有数据。示例简化为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]);
}
string[]Days=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt”);
字符串[]Depths=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt”);
...
string[]newArray=新字符串[Days.Length];
对于(int x=0;x
我会用一个新数据对象列表来解决这个问题。您可能希望在每个数组中循环并创建一个具有相同迭代器的新对象
首先创建新对象:
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;
}
public IEnumerable MyObjectBuilder()
{
//声明返回对象
var result=新列表();
//获取您的数据
string[]Days=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt”);
字符串[]Depths=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt”);
字符串[]IRIS_ID=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”);
//等等。。。
//循环项目以生成对象
对于(var i=0;i,如果您只需压缩即可打印数据,您还可以:
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
值,您可以创建一个类来聚合要显示的所有字段,然后遍历所有元素,并为每次迭代创建该类的新实例并添加添加到列表中。类似于:
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);
}
类合并
{
公共整数天{get;set;}
公共整数深度{get;set;}
等
}
对于(int i=0;i
您也可以这样做
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));
string[]Days=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day_1.txt”);
字符串[]Depths=System.IO.File.ReadAllLines(@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth_1.txt”);
字符串[]IRIS_ID=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”);
列表结果=新列表();
(新[]{天,深度,IRIS_id,纬度}).ToList().ForEach(a=>result.AddRange(a));
这里有两种方法可以实现您想要的功能。第一种解决方案是使用数组(如您所要求),另一种是使用字典
在这两种情况下,请使用枚举定义数据文件类型:
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];
}
static readonly string[]FileSpecs=new string[]
{
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Day\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Depth\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\IRIS\u ID\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Latitude\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Longitude\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\magnize\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Month\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Region\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Time\u 1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Timestamp_1.txt”,
@“C:\Users\Illimar\Desktop\Algorithms and Comlexity2\Year\u 1.txt”
};
静态void Main(字符串[]参数)
{
字符串[][]数据=新字符串[FileSpecs.Length][];
//读取数据
对于(int i=(int)DataFileType.Days;我查看了Zip
中的显示,您不需要合并数组。您需要数组还是只显示特定格式的数据?您不想“合并”如果将数组合并为一个数组,则需要从数组中创建一个复杂的结构。请注意,您的问题是在重构数据之前,如果所有这些文件都包含数据“行”的“列”,那么与其将每个文件保存在不同的文件中,不如将其保存为文件中的行,不如对每个列使用某种形式的separator(通过这种方式,您可以使用String.Split)或者更好地序列化正确格式化的日期。是否有原因不能将数据存储在XML或CSV文件中,并将其作为节点或逗号分隔的值读回
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];
}