2014-11-08 61 views
-1

我遇到了一個項目異常「System.IndexOutOfRangeException」,並在這裏隔離到這個示例代碼塊。索引超出了數組的範圍。但索引是在範圍

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     string testStr = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"; 
     //split testStr into substrings representing each edge 
     string [] edges = testStr.Split(", ".ToCharArray()); 

     foreach(string edge in edges) 
     { 
      Console.Write(edge + "\n"); 
      char [] cEdge = edge.ToCharArray(); 
      char cost = cEdge[cEdge.Length - 1]; // what out of bounds? 
      Console.Write(cost); 
     } 
    } 
} 

此問題來自「char cost = cEdge [cEdge.Length - 1];」行。這對我來說沒有任何意義,因爲此時cEdge應該是一個長度爲3的數組。因此,在cEdge.Length - 1處的索引應該是索引2,並且位於數組的邊界內。我很困惑,也許我已經看過一些東西。感謝您的時間和幫助。

+0

如果cEdge的長度爲零,那麼'cEdge [cEdge.Length-1]'將產生一個越界異常。 – dbc 2014-11-08 07:10:10

回答

1

問題是當數組爲空時Length屬性將等於0,並且cEdge [0 - 1]最終會給你IndexOutOfRangeException。考慮索引陣列之前檢查Length

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     string testStr = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"; 
     //split testStr into substrings representing each edge 
     string [] edges = testStr.Split(", ".ToCharArray()); 

     foreach(string edge in edges) 
     { 
      Console.Write(edge + "\n"); 
      char [] cEdge = edge.ToCharArray(); 
      if (cEdge.Length > 0) 
      { 
       char cost = cEdge[cEdge.Length - 1]; // what out of bounds? 
       Console.Write(cost); 
      } 
     } 
    } 
} 
3

默認情況下,Split方法包括在陣列中的空字符串。所以你是cEdge數組的大小是17,其中大約8個元素是空字符串。因此,當您嘗試迭代數組時,空字符串的長度爲0,並且您嘗試減去1,這會讓您超出數組的範圍。

這裏有幾個選項。您可以放置​​if語句以確保cEdge的長度爲3個字符,或更新Split方法以使用其中一個重載將自動刪除這些空元素。

這裏是你將如何使用過載:

string[] edges = testStr.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 

編輯

剛剛意識到我真的沒有解釋爲什麼你獲得額外的空組。出現空元素是因爲您爲分割函數提供了一個分隔符數組。該函數然後使用該數組匹配任何一個分隔符,並將其分解爲獨特的元素。在這種情況下,有一個很好的例子,那就是有所作爲。如果您的源字符串testStr要在其中一個字段中包含空格,則實際上會將該字段分成兩半,因爲您向分隔符列表中提供了空格。

作爲MSDN article所說:

分離器中的每個元素定義了單獨的分隔符。如果兩個分隔符相鄰,或者在此實例的開頭或結尾處找到分隔符,則相應的數組元素將包含空白。

因此,例如:

string testStr = "AB5, BC4, CD8 Test, DC8"; 
string [] edges = testStr.Split(", ".ToCharArray()); 

在這種情況下,大多數人認爲我們最終會得到一個數組,看起來是這樣的:

+----------------------------+ 
| AB5 | BC4 | CD8 Test | DC8 | 
+----------------------------+ 

但是這樣做的實際輸出方法會更像這樣:

+------------------------------+ 
| AB5 | BC4 | CD8 | Test | DC8 | 
+------------------------------+ 

要每次都獲得所需的輸出,一個更強大的解決方案會是這個樣子:

String[] edges = Regex.Split(testStr, Regex.Escape(", "), RegexOptions.None); 

如果分割恰好是一個緊密的循環中,你可能要考慮進入循環之前編譯正則表達式,但這是一個不同的問題。

0

因爲第2,第4,第6等...陣列中的數值爲空。

static void Main(string[] args) 
    { 
     string testStr = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"; 
     //split testStr into substrings representing each edge 
     string[] edges = testStr.Split(", ".ToCharArray()); 
     char[] cEdge; 
     foreach (string edge in edges) 
     { 
      Console.Write(edge + "\n"); 
      cEdge = edge.ToCharArray(); 
      char cost = new char(); 
      if(cEdge.Length > 0) 
      { 
       cost = cEdge[0]; // what out of bounds? 
      } 
      Console.Write(cost); 
     } 
     Console.Read(); 
    } 
0

我跟蹤你的代碼,我發現邊緣值包含""導致cEdge.Length是0 你一定要固定你這樣的代碼:

public static void Main() 
     { 
      string testStr = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"; 
      //split testStr into substrings representing each edge 
      string[] temp = { ", " }; 
      string[] edges = testStr.Split(temp, StringSplitOptions.RemoveEmptyEntries); 

      foreach (string edge in edges) 
      { 
       Console.Write(edge + "\n"); 
       char[] cEdge = edge.ToCharArray(); 
       char cost = cEdge[cEdge.Length - 1]; // what out of bounds? 
       Console.Write(cost); 
      } 
     } 
0

您的開裂的字符數組的字符串「」以及'
和你喜歡的東西:
[ 「AB5」, 「」, 「BC4」, 「」,...]
看這裏http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx
在這一行比邊==「」
char cost = cEdge [cEdge.Length - 1];
cEdge.Length == 0
,你會得到System.IndexOutOfRangeException 應使用以下語法
testStr.Split( 「」 .ToCharArray(),StringSplitOptions.RemoveEmptyEntries);