1

我一直在努力解决这个问题。运行此代码时,我不断让索引超出范围。

基本上,我拿了一个文本框,将其拆分为一个数组,然后使用数组的每个索引与一个充满字符串的数组进行比较。贴上相关代码,你们能看出我做错了吗?

我已经在错误点附近设置了一个错误。( <----- )

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++;
            }

        }



        }
4

3 回答 3

0

一些事情:

这一行是你的错误:
WordsIndex = 1; //我认为这个问题很简单,为什么它从 1 开始?它应该为零

counterWord 也应该从 0 开始,并且您应该在以下情况下停止迭代:
counterSearch < wordsplit.Length 而不是 counterSearch != wordsplit.Length

如果这段代码同时写在这两个范围内if,并且else应该移到两个范围之后:

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

固定代码:

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++;                
        }
        }
于 2013-10-15T05:23:02.477 回答
0

问题出在这一行

while (counterSearch != wordsplit.Length)

您不会更改两者的值,也counterSearch不会wordsplit因此循环无限期地运行,最终导致索引超出范围。

于 2013-10-15T05:23:14.403 回答
0

在开始 while 循环之前尝试将 WordsIndex 和/或 CounterWord 设置为 0 而不是 1。这可能是 IndexOutOfBounds 错误的原因,因为数组是零索引的。

于 2013-10-15T05:24:50.667 回答