還是新的C#所以要溫柔:)C#列表到數組問題
我有一些代碼讀取CSV文件並將輸出存儲在列表中。由於CSV中的條目數量未確定,因此我使用了List而不是Array。一旦從文件創建了列表,然後我想將它傳遞給Shell排序方法,因爲這是程序的目的。爲此,我首先要將列表轉換爲整數數組。我試過.ToArray
方法,但它似乎並沒有爲我工作。我得到一個例外
不能將Implicilty轉換類型
String[][]
到string[]
。
我知道我在做一些愚蠢的事情,但似乎無法找到什麼...任何幫助,將不勝感激。
//Import DAT file and format it.
public int[] ImportDat(string path)
{
List<string[]> loadedData = new List<string[]>();
loadedData.Clear();
try
{
using (StreamReader readCSV = new StreamReader(path))
{
string line;
string[] row;
while ((line = readCSV.ReadLine()) != null)
{
row = line.Split(',');
loadedData.Add(row);
}
}
}
catch
{
MessageBox.Show("Import Failed. Please check the file is in the same folder as the executable");
}
string[] MyArray = loadedData.ToArray();
//temp Array to return a value
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
return numbers;
}
將每行只有一個條目(即一列)或多列?如果後者,你想怎麼做?對行進行排序然後對列進行排序,或者將行中的所有條目添加到公共列表中,然後對整個列表進行排序? – GrayWizardx 2011-03-12 21:52:06