我在项目上遇到异常“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 :(得分:3)
默认情况下,Split
方法在数组中包含空字符串。所以你的cEdge
数组的大小为17,其中大约8个元素是空字符串。因此,当您尝试遍历数组时,空字符串的长度为0,并且您尝试减去1,这会使您超出数组的范围。
你在这里有几个选择。您可以放置一个if语句以确保cEdge
的长度为3个字符,或者更新Split方法以使用其中一个会自动删除这些空元素的重载。
以下是如何使用重载:
string[] edges = testStr.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
修改强>
刚才意识到我并没有真正解释为什么你得到额外的空组。出现空元素是因为您为split函数提供了一个分隔符数组。然后,该函数使用数组在任何一个分隔符上匹配,并将其分解为唯一元素。在这种情况下,这是一个完美的例子。如果您的源字符串testStr
要在其中一个字段中包含空格,那么它实际上会将该字段分成两半,因为您在分隔符列表中提供了一个空格。
正如MSDN article所说:
分隔符的每个元素都定义了一个单独的分隔符。如果两个分隔符相邻,或者在此实例的开头或结尾处找到分隔符,则相应的数组元素包含Empty。
例如:
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);
如果拆分恰好处于紧密循环中,您可能需要考虑在进入循环之前编译正则表达式,但这是一个不同的问题。
答案 1 :(得分: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);
}
}
}
}
答案 2 :(得分: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();
}
答案 3 :(得分: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);
}
}
答案 4 :(得分: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);