我在.NET下写一个CONSOLE的程序,就是把文件名为FILE_NAME的文件的第一行写到数组里面
StreamReader l_file = File.OpenText(FILE_NAME);
String l_txt;char[] pp = new char[50];
问题处char[,] p_String = new char[10,500]; //数组l_txt = l_file.ReadLine();
l_txt.CopyTo(0, pp, 0, l_txt.Length);//成功写入数组pp
去掉问题处,程序是运行正常的,
加上问题处,l_txt.CopyTo(0,pp[1],0,l_txt,Length);//运行出错,提示:内引参数为2请问我错在哪里了?
StreamReader l_file = File.OpenText(FILE_NAME);
String l_txt;char[] pp = new char[50];
问题处char[,] p_String = new char[10,500]; //数组l_txt = l_file.ReadLine();
l_txt.CopyTo(0, pp, 0, l_txt.Length);//成功写入数组pp
去掉问题处,程序是运行正常的,
加上问题处,l_txt.CopyTo(0,pp[1],0,l_txt,Length);//运行出错,提示:内引参数为2请问我错在哪里了?
char[][] p_String = new char[10][];
for (int i = 0; i < 10; ++ i)
{
p_String[i] = new char[500];
}
同意……~……·
去看看文档CopyTo(0, pp, 0, l_txt.Length)
这个方法的参数都要求什么
不久搞定了!~~
楼主用下面样式的数组试试
char[][] p_String = new char[10][];
for (int i = 0; i < 10; ++ i)
{
p_String[i] = new char[500];
}
============================================
谢谢你,我用这种方法试了的确是可以的,l_txt.CopyTo(0, p_String[1],0,l_txt,Length);
这样就可以了,但是我还是不能明白
char[,] p_String = new char[10,500];
l_txt.CopyTo(0, p_String[1],0,l_txt,Length); //提示错误:参数不能不为1
或者l_txt.CopyTo(0, p_String[1,],0,l_txt,Length); //提示错误:错误的]
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/csref/html/vcwlkArraysTutorial.htm
l_txt.CopyTo(0,pp[1],0,l_txt,Length);//运行出错,提示:内引参数为2这里的pp应当是p_String 吧?!
-----------------------------------
对。
我的理解是:
二维数组,当数组元素单个使用时,可以这样申请char[,] p_String = new char[10,500]; 和使用;
当数组以行形式使用时候,
char[][] p_String = new char[10][];
for (int i = 0; i < 10; ++ i)
{
p_String[i] = new char[500];
}
这样使用行,就可以直接使用p_String[i]了如果各位有不同意见请联系我,谢谢。