这里有两种方法可以实现您想要的。如您所问,第一个解决方案是使用数组,另一个使用字典。
在任何一种情况下,使用枚举定义数据文件类型:
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];
}
两者都不是最终的解决方案,但应该会给你一些想法。