0,       40.123,     -1.231,    10.59,
1,       40.230,     -1.242,    10.51,
2,       40.235,     -1.2425,    0,   
3,       40.545,     -1.9483,   10.35,   
4,       40.015,     -1.0473,   11.00,     
text文件中存放这样一组数据。运行时输入两个点(例如0,1),计算这两个点的距离。
我先把上述内容存放在list中,然后split存放在二维数组Coor。
可是现在运行有错误,不知道错哪了。
错误提示:未经处理的异常: system.IndexOutOfRangeException:索引超出的数组范围static void Main(string[] args)
        {
            
            //string path=@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt";
            //string[] readText = File.ReadAllLines(path);
            //Console.WriteLine("Point   Latitude   Longitude   Evelation");
            //foreach (string s in readText)
            //{
            //    Console.WriteLine(s);
            //} 
            
            // string[][] Coor;    //存放坐标的二维数组
            //  Coor = new string[5][];
           
            List<string[]> list = new List<string[]>();
            TextReader tr = new StreamReader(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
            
            int line_total = 0;                 //总行数
            while (tr.ReadLine() != null)
            {
                line_total++;
            }
            Console.WriteLine(line_total);     //先输出一下总行数            string rd = "";
            while ((rd = tr.ReadLine()) != null)
            {
                string[] row = tr.ReadLine().Split(',');
                string[] col = new string[row.Length];
                for (int i = 0; i < row.Length; i++)
                {
                    col[i] = Convert.ToString(row[i]);                }
                list.Add(col);            }            string[][] Coor = list.ToArray();
            Console.WriteLine(Coor[0][0]);
            
        }

解决方案 »

  1.   

    不用搞那么复杂,只要4行代码就行了:
    string[] rows = File.ReadAllLines(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
    string[][] values = new string[rows.Length][];
    for (int i = 0; i < rows.Length; i++)
    values[i] = rows[i].Split(',');
    //以下是输出各元素的值
    for (int i = 0; i < values.Length; i++)
    {
    for (int j = 0; j < values[i].Length; j++)
    Console.Write(values[i][j] + "    ");
    Console.WriteLine();
    }
      

  2.   

    string[] lines = File.ReadAllLines("路径");
    string[][] mylist = new string[lines.Length][];
    for (int i = 0; i < lines.Length; i++)
    {
        mylist[i] = lines[i].Split(',');
    }
      

  3.   

    啊,看到每行最后还多了个逗号,那么改成这样:
    string[] rows = File.ReadAllLines(@"E:\nottingham\study\PRG\Projects\distance_P2P\distance_P2P\Coordinate.txt");
    string[][] values = new string[rows.Length][];
    for (int i = 0; i < rows.Length; i++)
    values[i] = rows[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    //以下是输出各元素的值
    for (int i = 0; i < values.Length; i++)
    {
    for (int j = 0; j < values[i].Length; j++)
    Console.Write(values[i][j] + "    ");
    Console.WriteLine();
    }