循环遍历二维锯齿状数组的每个值 "Index was outside the bounds of the array"

issue looping through each value of a 2D Jagged Array "Index was outside the bounds of the array"

我从一个文本文件生成一个二维锯齿状数组,该数组是段落 > 句子,这个数组的格式正确,我可以在外部指定一个值,例如 [0][0] 等,它会显示正确。

然而,当我尝试在循环中执行此操作时,我在尝试显示 "results[0][0]".

时得到 "Index was outside the bounds of the array"

下面是数组生成代码:

string documentPath = @"I:\Project\Test Text.txt";
string content;
string[][] results;

protected void sentenceSplit()
{
    content = Regex.Replace(File.ReadAllText(documentPath), @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
    var paragraphs = content.Split(new char[] { '\n' });
    results = new string[paragraphs.Length][];
    for (int i = 0; i < results.Length; i++)
    {
        results[i] = Regex.Split(paragraphs[i], @"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s");
    }
}

这是循环代码:

protected void Intersection()
{
    for (int i = 0; i < results.Length; i++)
    {
        for (int s = 0; s < results.Length; s++)
        {
            TextboxSummary.Text += results[i][s];
        } 
    } 
}

我之前没有对二维数组做过太多工作,但我觉得这应该可行,当它经过测试时,它不会向文本框输出任何内容,即使它应该从 [0][0] 开始,这当然是保存数据,[1][1] 也保存数据,如果它以某种方式跳到那个。

正如我在评论中所说,您的代码在内部循环中获取了错误的数组长度。您的内部循环应该获取位于外部循环索引处的数组的长度。像这样:

for (int i = 0; i < results.Length; i++)
{
    // Get the length of the array that is at index i
    for (int s = 0; s < results[i].Length; s++)
    {
        TextboxSummary.Text += results[i][s];
    } 
}