C#列表到数组问题

时间:2011-03-12 21:46:15

标签: c#

还是C#的新手,所以要温柔:)

我有一些代码可以读取CSV文件并将输出存储在List中。我使用了List而不是Array,因为CSV中的条目数未确定。从文件创建List后,我想将它传递给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;
}

6 个答案:

答案 0 :(得分:4)

您似乎真的想要CSV文件中的一维数字列表,但您现在正在单独处理每一行。如果是这种情况,请使用:

 loadedData.AddRange(row);

而不是:

 loadedData.Add(row);

并将loadedData声明为List<string>。现在你仍然需要转换为int,因为你的方法返回一个int列表。你可以使用LINQ:

List<int> results = loadedData.Select(s=> Convert.ToInt32(s)).ToList();

你的方法也可以用LINQ完全表达出来:

public int[] ImportDat(string path)
{
    List<int> result = File.ReadAllLines(path)
                           .Select(line => line.Split(','))
                           .SelectMany(s => s)
                           .Select( s=> Convert.ToInt32(s))
                           .ToList();
    return result;
}

答案 1 :(得分:1)

您的变量loadedData是一个字符串数组的列表。在它上面调用.ToArray()将返回一个字符串[] [](2维数组),而不是字符串[]。

答案 2 :(得分:1)

为什么不立即使用列表并立即转换数字?

public int[] ImportDat(string path)
{
     List<int> loadedData = new List<int>();

     loadedData.Clear();

     try
     {
        using (StreamReader readCSV = new StreamReader(path))
        {
            string line;
            string[] row;

            while ((line = readCSV.ReadLine()) != null)
            {
                row = line.Split(',');
                foreach(var token in row)
                {
                    loadedData.Add(Convert.ToInt32(token));
                }
            }
        }
     }
     catch
     {
        MessageBox.Show("Import Failed. Please check the file is in the same folder as the executable");
     }

     return loadedData.ToArray();
}

答案 3 :(得分:0)

您将loadedData声明为字符串数组列表。你应该做的

List<string> loadedData = new List<string>();

答案 4 :(得分:0)

问题是List中的每个项目都是一个字符串数组。 它不是字符串列表。 你应该让你的“MyArray”成为一个二维的字符串数组。 换句话说,一串字符串数组。 所以简而言之:string [] [] MyArray = loadedData.ToArray();

答案 5 :(得分:0)

也许您可以修改此代码以满足您的需求?

   List<int> numbersList = new List<int>();
        string input = "2,4,6,3";
        string[] numbersStringArray  = input.Split(',');
        foreach (var numberString in numbersStringArray)
        {
            numbersList.Add(Convert.ToInt32(numberString));
        }

        int[] numbersArray = numbersList.ToArray<int>();