繁体   English   中英

获取“索引超出数组范围”异常

[英]Getting “Index was outside the bounds of the array” exception

我们有一个数据表“ 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();
    }

再次向所有人致谢。 问候,萨加尔

您基于i的方式是行计数(即X)而不是字符串数组的长度。 因此,您可能有100行,但是该字符串仅分为5个字符串值。

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...

您需要为该行中的单词数使用有效的索引值-尚不清楚您要在代码中确切实现什么,因此我无法提供示例。

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

如果您有一个假设行“ abcde”(例如文件的第一行),而数据库“ st”包含10行,则将在整个过程中循环10次,第六次遇到此错误。 第6步将字符串拆分为:{a,b,c,d,e},然后尝试获取第5个索引(从零开始),该索引位于数组之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM