简体   繁体   中英

C# Index out of bounds of array

I am reading line by line from a textfile and i will populate 100 textbox from the textfile ( 10x10 line ) there is 10 comma in each line and there is 10 line in the textfile so i am trying to read the value from textfile . But however it shows Index was out of bounds , any help?

string[] fileData = File.ReadAllLines(@"C:\Users\omgjyan\Desktop\OneOrganizer\OneOrganizer\WordPuzzle\educational.txt");

string[] lineValues;

int row = 0;
int col;

string[][] rowcol = new string[fileData.Length][];

foreach (string line in fileData)
{
    lineValues = line.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
    rowcol[row] = new string[lineValues.Length];

    col = 0;

    foreach (string value in lineValues)
    {   
        rowcol[row][col] = value;                          
        col++;
    }

    row++;

}

for (int i = 0; i < rowcol.GetLength(0); i++)
{
    for (int j = 0; j < rowcol[i].GetLength(0); j++)
    {
        TextBox tbox = new TextBox();

        int iadd = i + 1;
        int iminus = i - 1;
        int jadd = i + 1;
        int jminus = i - 1;
        var self = rowcol[i][j];
        var top = rowcol[iminus][j];
        var bottom = rowcol[iadd][j];
        var left = rowcol[i][jminus];
        var right = rowcol[i][jadd];

        if ((!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(left)) ||
                            (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(left))
                        )
    {
        tbox.Text = "*";
    }

    wrapPanel1.Children.Add(tbox);
    }
}

I got the error saying index was out of bounds . Could it be the +1 and -1? how do i solve that

error : 在此输入图像描述

Your code has:

for (int i = 0; i < rowcol.GetLength(0); i++)
{
    for (int j = 0; j < rowcol[i].GetLength(0); j++)
    {
        int iadd = i + 1;
        int iminus = i - 1;
        int jadd = i + 1;
        int jminus = i - 1;

And then you're trying to access the arrays with

var self = rowcol[i][j];
var top = rowcol[iminus][j];
var bottom = rowcol[iadd][j];
var left = rowcol[i][jminus];
var right = rowcol[i][jadd];

This will on all the edge cases because iminus will be one -1 , and iadd will rowcol.Length , and the same for jminus / jadd . These indexes are outside the bounds of your arrays. You can fix this by simply discarding the edge cases in your loop:

for (int i = 1; i < rowcol.Length - 1; i++)
{
    for (int j = 1; j < rowcol[i].Length - 1; j++)
    {
        int iadd = i + 1;
        int iminus = i - 1;
        int jadd = i + 1;
        int jminus = i - 1;

This will ensure that you are not accessing any elements outside the array's boundaries. But this alone will not be enough to ensure the exception never occurs. Look again at these lines:

var top = rowcol[iminus][j];
var bottom = rowcol[iadd][j];

This this will fail if the length of rowcol[iminus] or rowcol[iadd] is less than rowcol[i] . So you will need to take special care to avoid this as well. Perhaps something like this:

for (int i = 1; i < rowcol.Length - 1; i++)
{
    for (int j = 1; j < rowcol[i].Length - 1; j++)
    {
        int iadd = i + 1;
        int iminus = i - 1;
        int jadd = i + 1;
        int jminus = i - 1;
        if (j < rowcol[iminus].Length || j < rowcol[iadd].Length)
        {
            continue;
        }

Another way you could try this would be something like this:

for (int i = 0; i < rowcol.Length; i++)
{
    for (int j = 0; j < rowcol[i].Length; j++)
    {
        int iadd = i + 1;
        int iminus = i - 1;
        int jadd = i + 1;
        int jminus = i - 1;
        var self = rowcol[i][j];
        var top = iminus >= 0 && j < rowcol[iminus].Length ? rowcol[iminus][j] : string.Empty;
        var bottom = iadd < rowcol.Length && j < rowcol[iadd].Length ? rowcol[iadd][j] : string.Empty;
        var left = jminus >= 0 ? rowcol[i][jminus] : string.Empty;
        var right = jminus < rowcol[i].Length ? rowcol[i][jadd] : string.Empty;

When i and j are 0, i - 1 and j - 1 are both -1, which will cause out of bounds when you try:

    int iadd = i + 1; 
    int iminus = i - 1;
    int jadd = i + 1;
    int jminus = i - 1;
    var self = rowcol[i][j]; 
    var top = rowcol[iminus][j]; //iminus is -1 first iteration!
    var bottom = rowcol[iadd][j];
    var left = rowcol[i][jminus]; //jminus is -1 first iteration!
    var right = rowcol[i][jadd];

You can solve this by adding checks that the indices are, in fact in bounds (and skip iterations where they aren't).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM