簡體   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