C#中不断得到索引超出范围使用&QUOT时,阵列[X]。载有(数组2 [Y]" [英] c# Keep getting index is out of bounds when using "array[x].contains(array2[y]"

查看:168
本文介绍了C#中不断得到索引超出范围使用&QUOT时,阵列[X]。载有(数组2 [Y]"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲打我的脑袋上这一段时间。运行此code时,我不断收到索引越界..

基本上,我参加了一个文本框,把它分解成一个数组,然后使用数组中的每个指标来比较完整的字符串数组。粘贴相关code,你们可以看到我做错了什么?

我给自己定把一个错误的错误点附近。 (小于-----)

 公共部分类主窗口:窗口
{    字符串[] = kbsubject新的字符串[4000];
    字符串[] = kbbody新的字符串[4000];
    字符串[] = wordsplit新的字符串[4000];
    INT [] =命中INT新[4000];
     StreamWriter的WriteBody =新的StreamWriter(kbsubjecttest.txt);
    StreamReader的readSubject =新的StreamReader(kbsubject.txt);
    StreamReader的readBody =新的StreamReader(kbbody.txt);
    INT IndexHolder = 0,计数器= 0,counterSearch = 0,WordsIndex = 0,泛意语= 0,ArrayIndex = 0;
    串compareBody,compareSubject;
    公共主窗口()
    {
        的InitializeComponent();
    }    私人无效的button1_Click(对象发件人,RoutedEventArgs E)
    {
        wordsplit = SearchBox.Text.Split('');
        diagWindow.Items.Add(wordsplit.Length);
        diagWindow.Items.Add(preforming通过拆分搜索);
        WordsIndex = 1;
        泛意语= 1;
        而(counterSearch!= wordsplit.Length)
        {
            如果(kbbody [泛意语]。载(wordsplit [WordsIndex]))≤; --------
            {
                点击[ArrayIndex] =泛意语;
                ArrayIndex ++;
                泛意语++;
                WordsIndex ++;
            }
            其他
            {
                ArrayIndex ++;
                泛意语++;
                WordsIndex ++;
            }        }        }


解决方案

几件事情:

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

泛意语应该从0开始,以及,什么时候应该停止迭代:结果
counterSearch< wordsplit.Length 而非 counterSearch!= wordsplit.Length

如果这个code被写入在两个如果其他就应该转移到这两个范围后:

  ArrayIndex ++;
泛意语++;
WordsIndex ++;

固定code:

 公共部分类主窗口:窗口
{    字符串[] = kbsubject新的字符串[4000];
    字符串[] = kbbody新的字符串[4000];
    字符串[] = wordsplit新的字符串[4000];
    INT [] =命中INT新[4000];
     StreamWriter的WriteBody =新的StreamWriter(kbsubjecttest.txt);
    StreamReader的readSubject =新的StreamReader(kbsubject.txt);
    StreamReader的readBody =新的StreamReader(kbbody.txt);
    INT IndexHolder = 0,计数器= 0,counterSearch = 0,WordsIndex = 0,泛意语= 0,ArrayIndex = 0;
    串compareBody,compareSubject;
    公共主窗口()
    {
        的InitializeComponent();
    }    私人无效的button1_Click(对象发件人,RoutedEventArgs E)
    {
        wordsplit = SearchBox.Text.Split('');
        diagWindow.Items.Add(wordsplit.Length);
        diagWindow.Items.Add(preforming通过拆分搜索);
        WordsIndex = 0;
        泛意语= 0;
        而(counterSearch< wordsplit.Length)
        {
            如果(kbbody [泛意语]。载(wordsplit [WordsIndex]))≤; --------
            {
                点击[ArrayIndex] =泛意语;
            }            ArrayIndex ++;
            泛意语++;
            WordsIndex ++;
        }
        }

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

        }



        }

解决方案

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

这篇关于C#中不断得到索引超出范围使用&QUOT时,阵列[X]。载有(数组2 [Y]&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆