- 在线获取异常
convertedMatrix[temp] = matrix[i,j]
; - 确切的错误:
IndexOutOfRangeException:数组索引超出范围。System.Text.StringBuilder.set_Chars(Int32 索引,Char 值)(在 /System.Text/StringBuilder.cs:行号)
这是代码(在 C# 中):
public const int size = 4; public System.Text.StringBuilder convertedMatrix = new System.Text.StringBuilder(size * size); public char[,] matrix = new char[,]{'i','s','e','m','r','v','u','n','t','o','d','a'}; public void Generate() { for(int i = 0; i < size; i++) { convertedMatrix.Append(" "); } int temp = 0; for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { convertedMatrix[temp] = matrix[i,j]; temp += 1; } } }
问问题
345 次
2 回答
2
有字符convertedMatrix
。i
在您收到错误的行中,temp
最多可以达到 (i-1)*(j-1),它大于 i。
您可能想增加convertedMatrix
.
于 2016-12-04T09:15:10.687 回答
0
抱歉各位调试了。正如@yper 所说,convertedMatrix 的字符更少。所以错误就在下面:
for(int i = 0; i < size; i++) {
wordsLength[i] = 0;
convertedMatrix.Append(" ");
}
相反,它应该是:
for(int i = 0; i < size * size; i++) {
wordsLength[i] = 0;
convertedMatrix.Append(" ");
}
多谢你们!
于 2016-12-04T10:05:37.617 回答