从text读内容存放二维数组的问题。急

right89757 2012-10-23 06:18:18
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]);

}
...全文
167 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2012-10-23
  • 打赏
  • 举报
回复
啊,看到每行最后还多了个逗号,那么改成这样:
		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();
}
wuyq11 2012-10-23
  • 打赏
  • 举报
回复
string[] lines = File.ReadAllLines("路径");
string[][] mylist = new string[lines.Length][];
for (int i = 0; i < lines.Length; i++)
{
mylist[i] = lines[i].Split(',');
}
dalmeeme 2012-10-23
  • 打赏
  • 举报
回复
不用搞那么复杂,只要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();
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧