获取“索引超出数组范围”例外
我们有一个数据表“st”,其中有两列“word”和“binary”,
void replace()
{
string s1="", s2="";
StreamReader streamReader;
streamReader = File.OpenText("C:\\text.txt");
StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
int x = st.Rows.Count;
// int i1 = 0;
// Now, read the entire file into a string
while ((line = streamReader.ReadLine()) != null)
{
for (int i = 0; i < x; i++)
{
s1 = Convert.ToString(st.Rows[i]["Word"]);
s2 = Convert.ToString(st.Rows[i]["Binary"]);
s2+="000";
char[] delimiterChars = { ' ', '\t' };
String[] words = line.Split(delimiterChars);
// Write the modification into the same file
String ab = words[i]; //exception occurs here
// Console.WriteLine(ab);
streamWriter.Write(ab.Replace(s1,s2));
}
}
streamReader.Close();
streamWriter.Close();
}
我们收到“索引超出数组范围”异常。我们无法找到问题所在。提前致谢
编辑: 致所有提供帮助的人。 我这样做了,并且以某种方式奏效了:
void replace()
{
string s1 = "", s2 = "";
StreamReader streamReader;
streamReader = File.OpenText("C:\\sample1.txt");
StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
int x = st.Rows.Count;
while ((line = streamReader.ReadLine()) != null)
{
char[] delimiterChars = { ' ', '\t' };
String[] words = line.Split(delimiterChars);
foreach (string str in words)
{
s1 = str;
DataRow drow = st.Rows.Find(str);
if (drow != null)
{
index = st.Rows.IndexOf(drow);
s2 = Convert.ToString(st.Rows[index]["Binary"]);
// s2 += "000";
// ab = words[i];
Console.WriteLine(s1);
Console.WriteLine(s2);
streamWriter.Write(str.Replace(s1, s2));
}
else
break;
}
}
streamReader.Close();
streamWriter.Close();
}
再次向大家发送信息。 问候, 萨加尔
we have a datatable "st" with two columns "word" and "binary"
void replace()
{
string s1="", s2="";
StreamReader streamReader;
streamReader = File.OpenText("C:\\text.txt");
StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
int x = st.Rows.Count;
// int i1 = 0;
// Now, read the entire file into a string
while ((line = streamReader.ReadLine()) != null)
{
for (int i = 0; i < x; i++)
{
s1 = Convert.ToString(st.Rows[i]["Word"]);
s2 = Convert.ToString(st.Rows[i]["Binary"]);
s2+="000";
char[] delimiterChars = { ' ', '\t' };
String[] words = line.Split(delimiterChars);
// Write the modification into the same file
String ab = words[i]; //exception occurs here
// Console.WriteLine(ab);
streamWriter.Write(ab.Replace(s1,s2));
}
}
streamReader.Close();
streamWriter.Close();
}
we're getting an "Index was outside the bounds of the array" exception. we're unable to find the problem. thanks in advance
EDIT:
tnx to everyone who helped.
i did this and it worked somehow:
void replace()
{
string s1 = "", s2 = "";
StreamReader streamReader;
streamReader = File.OpenText("C:\\sample1.txt");
StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
int x = st.Rows.Count;
while ((line = streamReader.ReadLine()) != null)
{
char[] delimiterChars = { ' ', '\t' };
String[] words = line.Split(delimiterChars);
foreach (string str in words)
{
s1 = str;
DataRow drow = st.Rows.Find(str);
if (drow != null)
{
index = st.Rows.IndexOf(drow);
s2 = Convert.ToString(st.Rows[index]["Binary"]);
// s2 += "000";
// ab = words[i];
Console.WriteLine(s1);
Console.WriteLine(s2);
streamWriter.Write(str.Replace(s1, s2));
}
else
break;
}
}
streamReader.Close();
streamWriter.Close();
}
tnx to everyone once again.
regards,
sagar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您基于 i 的方式是行计数(又名 X)的数量,而不是字符串数组的长度。因此,您可能有 100 行,但字符串仅拆分为 5 个字符串值。
The way you are basing off i is the number of row count aka X and not the length of the string array. Therefore you might have 100 rows but the string is split into only 5 string values.
i
是您所在行的索引。words
包含该行上单词的数组。如果行中的单词少于行索引...您将收到越界异常。
您需要为该行中的单词数使用有效的索引值 - 目前尚不清楚您到底想在代码中实现什么目标,所以我无法提供示例。
i
is the index of the line you are on.words
contains an array of the words on that line.If there are less words in the line than the line index... you get an out of bounds exception.
You need to use a valid index value for the number of words in that line - it is not clear what exactly you are trying to achieve in your code, so I can't provide an example.
如果您有一个假设的行“abcd e”,例如文件中的第一行,并且您的数据库“st”包含 10 行,则您将循环该过程 10 次,在第 6 次时您将遇到错误。第六步将字符串拆分为:{a,b,c,d,e},然后尝试获取第五个索引(从零开始),该索引位于数组之外。
If you have a hypothetical line "a b c d e" for instance the first line in your file, and your database "st" contains 10 rows, you would loop through the process 10 times, on the 6th time you would encounter the error. The 6th step would split the string into: {a, b, c, d, e} and then attempt to get the 5th index (zero based), which would be outside of the array.
使用调试器来查看
line
和words
数组中的内容,并将其与i
的值进行比较。调试器是你最好的朋友。Use the debugger, in order to look what is in
line
and in thewords
array and compare it to the value ofi
. The debugger is your best friend.