获取“索引超出数组范围”例外

发布于 2025-01-02 02:06:46 字数 2557 浏览 0 评论 0原文

我们有一个数据表“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

找回味觉 2025-01-09 02:06:46

您基于 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.

§对你不离不弃 2025-01-09 02:06:46

i 是您所在行的索引。

while ((line = streamReader.ReadLine()) != null)
{
   for (int i = 0; i < x; i++)  // say we are on line 20

words 包含该行上单词的数组。

String[] words = line.Split(delimiterChars); // say there are 3 words on line 20

如果行中的单词少于行索引...您将收到越界异常。

String ab = words[i]; // trying to get to word 20 out of 3...

您需要为该行中的单词数使用有效的索引值 - 目前尚不清楚您到底想在代码中实现什么目标,所以我无法提供示例。

i is the index of the line you are on.

while ((line = streamReader.ReadLine()) != null)
{
   for (int i = 0; i < x; i++)  // say we are on line 20

words contains an array of the words on that line.

String[] words = line.Split(delimiterChars); // say there are 3 words on line 20

If there are less words in the line than the line index... you get an out of bounds exception.

String ab = words[i]; // trying to get to word 20 out of 3...

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.

梦断已成空 2025-01-09 02:06:46

如果您有一个假设的行“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.

淡莣 2025-01-09 02:06:46

使用调试器来查看 linewords 数组中的内容,并将其与 i 的值进行比较。调试器是你最好的朋友。

Use the debugger, in order to look what is in line and in the words array and compare it to the value of i. The debugger is your best friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文