如何从文件中读取数据写入数组(急!!!谢谢各位)
一个*text文件中有一些数字,整齐地分为3列n行,列中间用空格隔开。如何读取这个文件并把每一列每一行的数字存入数组。谢谢各位了!! --------------------编程问答--------------------
string[] lines = File.ReadAllLines("");
List<int[]> ls = new List<int[]>();
foreach (string line in lines)
{
int[] numbers = line.Split(new char[' ']);
ls.Add(numbers);
}
大概这个意思
自己改改 --------------------编程问答-------------------- 按行读取,拆分字符串嘛,一楼的方法基本可以用 --------------------编程问答-------------------- 读取,拆分 --------------------编程问答-------------------- class Program
{
static void Main(string[] args)
{
string path = @"d:\hhk.txt";
string[] lines = File.ReadAllLines(path);
ArrayList ls = new ArrayList();
char[] delimiterChars = { ' ' };
foreach (string line in lines)
{
string[] numbers = line.Split(delimiterChars);
ls.Add(numbers);
}
foreach (string[] ss in ls)
{
Console.WriteLine(ss);
}
}
}
我运行以后,出来的结果却是:
system.string[]
system.string[]
system.string[]
system.string[]
system.string[]
是什么原因啊?? --------------------编程问答-------------------- foreach (string[] ss in ls)
{
Console.WriteLine(ss);
}
你打印的是数组 不是 string ss[0] ss[1] ss[2] --------------------编程问答-------------------- foreach (string[] ss in ls)
{
foreach(string s in ss)
Console.WriteLine(s);
}
--------------------编程问答-------------------- 你输出的是整个数组,而不是数组厘米的数据
class Program
{
static void Main(string[] args)
{
string path = @"d:\hhk.txt";
string[] lines = File.ReadAllLines(path);
ArrayList ls = new ArrayList();
char[] delimiterChars = { ',' };
foreach (string line in lines)
{
string[] numbers = line.Split(delimiterChars);
ls.Add(numbers);
}
foreach (string[] ss in ls)
{
for (int i = 0; i < ss.Length;i++ )
Console.WriteLine(ss[i]);
}
Console.Read();
}
}
补充:.NET技术 , C#