Reputation: 1414
I generate a 2D Jagged Array from a text file, the array is Paragraphs > Sentences, this array is formed correctly, and I can specify outside a value such as [0][0] etc and it'll display correctly.
However when I try and do this within the loop I get "Index was outside the bounds of the array" when trying to display "results[0][0]".
Below is the array generation code:
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");
}
}
And here is the loop code:
protected void Intersection()
{
for (int i = 0; i < results.Length; i++)
{
for (int s = 0; s < results.Length; s++)
{
TextboxSummary.Text += results[i][s];
}
}
}
I've not done much work with 2D arrays before but feel this should work, when its tested it doesn't output anything to the text box even though it should be starting on [0][0] which certainly holds data, also [1][1] holds data as well if its somehow skipping to that.
Upvotes: 0
Views: 235
Reputation: 28530
As I put in my comment, your code is grabbing the wrong array length in your inner loop. Your inner loop should be getting the length of the array that is at the index of the outer loop. like this:
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];
}
}
Upvotes: 3