2012-10-05 41 views
-4

我用List來存儲值,如果數據是有限的,它的工作正常,如果數據在計數中超過15o,那麼我得到以下錯誤 索引超出了數組的範圍...... Plz提出了一個想法來解決這個問題。如何解決這個錯誤:索引超出了數組的邊界

List<string> code = new List<string>(); 
private void btn_browse_Click(object sender, EventArgs e) 
{ 
    DialogResult fileopen = openFileDialog1.ShowDialog(); 
    string filename = openFileDialog1.FileName;   
    txt_filename.Text = filename; 
    try 
    { 
     StreamReader readtxtfile = new StreamReader(filename); 
     String line = null; 

     string str = null; 
     char[] separate = { ',' }; 
     string[] words; 
     while ((str = readtxtfile.ReadLine()) != null) 
     { 
      words = str.Split(separate);       
      code.Add(Convert.ToString(words[0]) + '-' + Convert.ToString(words[2]).Trim()); 
     }      
    } 
+2

...做出更大的數組?動態擴展它?如果您不想提供更多詳細信息,則無法提供幫助。 – nneonneo

+1

你能粘貼一些代碼嗎? – secondflying

+1

你能告訴我們一些你訪問數據的代碼嗎? – CrazyCasta

回答

1

此錯誤不是因爲您的列表,而是來自您的數組「單詞」。數組的第一個檢查長度這樣

words = str.Split(separate); 
if(words.Length>2) 
{      
    code.Add(Convert.ToString(words[0]) + '-' + Convert.ToString(words[2]).Trim()); 
} 

確保每當數組的長度小於2就不會在你的列表中添加。

希望這有助於...

+0

謝謝你manish,現在它的工作 –

1

很難幫忙看看沒有代碼。

檢查以確保您的indexOutOfBounds錯誤來自您的列表,而不是您的拆分字符串以及。

+0

嗨,我現在添加了代碼 –

相關問題