请高手看一段程序

cafay 2012-04-08 07:42:11
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] b = Matrix.ReadFromFile("C:\\1.txt");
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
}
}
public class Matrix
{
public static int[,] ReadFromFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
List<string> ls = new List<string>();
string str = sr.ReadLine();
while (str != null)
{
ls.Add(str);
str = sr.ReadLine();
}
sr.Close();
fs.Close();
if (ls.Count != 0)
{
int row = ls.Count;
int col = ls[0].Split(' ').Count();
int[,] array = new int[row, col];
for (int i = 0; i < row; i++)
{
string[] ss = ls[i].Split(' ');
for (int j = 0; j < ss.Count(); j++)
{
array[i, j] = int.Parse(ss[j]);
}
}
return array;
}
return null;
}
}
}
之后显示“System.Array”并不包含“Count”的定义这个问题,请问该如何修改呢?请教高手~~
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
WAN 2012-04-08
  • 打赏
  • 举报
回复
代码没错。但楼主是否明白你这段代码的含义?
C:\1.txt 文件的内容:
1 2 3
4 5 6
7 8 9
lkr2380869 2012-04-08
  • 打赏
  • 举报
回复
一步一步debug吧
cafay 2012-04-08
  • 打赏
  • 举报
回复
谢谢各位,我用vs2008,就没有此类问题,但是有了新的问题,就是运行时,为空的,难道是我的main()函数写错了?
WAN 2012-04-08
  • 打赏
  • 举报
回复
使用.Net Framework 3.5以上 -> Linq
或直接用Length
ttllxx 2012-04-08
  • 打赏
  • 举报
回复
数组只有Length

111,126

社区成员

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

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

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