這裏有兩種方式來完成你想要的東西。第一種解決方案是使用數組,如你所問,另一種使用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