c# Keep getting index is out of bounds when using “array[x].contains(array2[y]”

爱⌒轻易说出口 提交于 2019-12-11 10:47:35

问题


I've been banging my head on this for a while. I keep getting Index out of Bounds when running this code..

basically, I took a textbox, split it up into an array, then using each index of the array to compare to a array full of strings. Pasted relevant code, can you guys see what I did wrong?

I've set put an error near the point of error. ( <----- )

public partial class MainWindow : Window
{

    string[] kbsubject = new string[4000];
    string[] kbbody = new string[4000];
    string[] wordsplit = new string[4000];
    int[] hits = new int[4000];
     StreamWriter WriteBody = new StreamWriter("kbsubjecttest.txt");
    StreamReader readSubject = new StreamReader("kbsubject.txt");
    StreamReader readBody = new StreamReader("kbbody.txt");
    int IndexHolder = 0, counter = 0, counterSearch = 0, WordsIndex = 0, counterWord=0, ArrayIndex = 0;
    string compareBody, compareSubject;


    public MainWindow()
    {
        InitializeComponent();




    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        wordsplit = SearchBox.Text.Split(' ');
        diagWindow.Items.Add(wordsplit.Length);
        diagWindow.Items.Add("Preforming search by split");
        WordsIndex = 1;
        counterWord = 1;
        while (counterSearch != wordsplit.Length)
        {
            if (kbbody[counterWord].Contains(wordsplit[WordsIndex]))   <--------
            {
                hits[ArrayIndex] = counterWord;
                ArrayIndex++;
                counterWord++;
                WordsIndex++;




            }
            else
            {
                ArrayIndex++;
                counterWord++;
                WordsIndex++;
            }

        }



        }

回答1:


Few things:

This line is your bug:
WordsIndex = 1; // I think the issue it quiet simple, why it's starting from 1? it should be zero.

counterWord should start from 0 as well, and you should stop iterating when:
counterSearch < wordsplit.Length and not counterSearch != wordsplit.Length

If this code is written in both if and else it should moved to after both scopes:

ArrayIndex++;
counterWord++;
WordsIndex++;

Fixed code:

public partial class MainWindow : Window
{

    string[] kbsubject = new string[4000];
    string[] kbbody = new string[4000];
    string[] wordsplit = new string[4000];
    int[] hits = new int[4000];
     StreamWriter WriteBody = new StreamWriter("kbsubjecttest.txt");
    StreamReader readSubject = new StreamReader("kbsubject.txt");
    StreamReader readBody = new StreamReader("kbbody.txt");
    int IndexHolder = 0, counter = 0, counterSearch = 0, WordsIndex = 0, counterWord=0, ArrayIndex = 0;
    string compareBody, compareSubject;


    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        wordsplit = SearchBox.Text.Split(' ');
        diagWindow.Items.Add(wordsplit.Length);
        diagWindow.Items.Add("Preforming search by split");
        WordsIndex = 0;
        counterWord = 0;
        while (counterSearch < wordsplit.Length)
        {
            if (kbbody[counterWord].Contains(wordsplit[WordsIndex]))   <--------
            {
                hits[ArrayIndex] = counterWord;
            }

            ArrayIndex++;
            counterWord++;
            WordsIndex++;                
        }
        }



回答2:


The problem is in this line

while (counterSearch != wordsplit.Length)

You don't change the value of neither counterSearch nor wordsplit so the loop is running indefinitely, finally causing index out of bounds.




回答3:


Try setting WordsIndex and/or CounterWord to 0 instead of 1 before starting the while loop. It is possibly the cause of the IndexOutOfBounds error, because arrays are zero-indexed.



来源:https://stackoverflow.com/questions/19373717/c-sharp-keep-getting-index-is-out-of-bounds-when-using-arrayx-containsarray2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!