[英]c# Keep getting index is out of bounds when using “array[x].contains(array2[y]”
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? //我认为这个问题很简单,为什么要从1开始呢? it should be zero . 它应该为零 。
counterWord should start from 0 as well, and you should stop iterating when: counterWord也应从0开始,并且在以下情况下应停止迭代:
counterSearch < wordsplit.Length
and not counterSearch != wordsplit.Length
counterSearch < wordsplit.Length
而不是 counterSearch != wordsplit.Length
If this code is written in both if
and else
it should moved to after both scopes: 如果此代码同时在if
和else
编写,则应将其移到两个作用域之后:
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++;
}
}
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. 您无需更改counterSearch
或wordsplit
的值,因此循环将无限期运行,最终导致索引超出范围。
Try setting WordsIndex and/or CounterWord to 0 instead of 1 before starting the while loop. 在启动while循环之前,尝试将WordsIndex和/或CounterWord设置为0而不是1。 It is possibly the cause of the IndexOutOfBounds error, because arrays are zero-indexed. 这可能是IndexOutOfBounds错误的原因,因为数组的索引为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.