这里有两种方式来完成你想要的东西。第一种解决方案是使用数组,如你所问,另一种使用Dictionary。
在任一情况下,具有一个枚举定义数据的文件类型:
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是单调乏味的。
使用字典提供了更大的灵活性:
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];
}
也不是最终的解决办法,但应该给你一些想法。
看看'邮编' –
对于显示你不需要合并数组。你需要数组还是只显示特定格式的数据? – BWA
你不想将数组“合并”为一个,你想从数组中创建一个复杂的结构。 – Gusman