Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二维数组列表二进制搜索c#_C#_Windows Forms Designer - Fatal编程技术网

二维数组列表二进制搜索c#

二维数组列表二进制搜索c#,c#,windows-forms-designer,C#,Windows Forms Designer,被要求为通过字符串排序的2D数组创建一个二进制搜索方法。我所做的是作为一门课程的一部分,这门课程有很多细节,我不能使用任何预先构建的搜索方法,但我必须在没有任何学习内容的情况下创建它,我所得到的只是一个标准的二进制搜索方法,用于包含整数的一维数组,所以没有太多帮助 private void BtnSearch_Click(object sender, EventArgs e) { int startIndex = 0;

被要求为通过字符串排序的2D数组创建一个二进制搜索方法。我所做的是作为一门课程的一部分,这门课程有很多细节,我不能使用任何预先构建的搜索方法,但我必须在没有任何学习内容的情况下创建它,我所得到的只是一个标准的二进制搜索方法,用于包含整数的一维数组,所以没有太多帮助

        private void BtnSearch_Click(object sender, EventArgs e)
        {

            int startIndex = 0;
            int finalIndex = gameArray.Length -1;

            bool flag2 = false;
            string searchTerm = txtSearch.Text;
            int foundIndex = -1;


            while (!flag2 && !((finalIndex - startIndex) <= 1))
            {
                int middle = (finalIndex + startIndex) / 2;
                if (string.Compare(gameArray[middle, 0], searchTerm, true) == 0)
                {
                    foundIndex = middle;
                    flag2 = true;
                    break;
                }
                else
                {
                    if (string.Compare(gameArray[middle, 0], searchTerm, true) > 0)
                        finalIndex = middle;
                    else
                        startIndex = middle;
                }

            }
            if (flag2)
                lstGames.SelectedIndex = foundIndex;
            else
                MessageBox.Show("Not Found");

        }
private void BtnSearch\u单击(对象发送者,事件参数e)
{
int startIndex=0;
int finalIndex=gameArray.Length-1;
bool-flag2=假;
字符串searchTerm=txtSearch.Text;
int foundIndex=-1;
而(!flag2&&!((finalIndex-startIndex)0)
finalIndex=中等;
其他的
startIndex=中间;
}
}
如果(标志2)
lstGames.SelectedIndex=foundIndex;
其他的
MessageBox.Show(“未找到”);
}
每当我在程序中执行搜索时,都会收到此错误消息

System.IndexOutOfRangeException:“索引超出了数组的边界。”

这在While循环执行时发生

真的不太确定我在这一点上做了什么不正确。

在测试之前,您应该检查“middle”是否等于或大于数组的长度

避免使用!((finalIndex startIndex)1)。太干净了

!flag2在while子句中是不必要的,因为只有在flag2为true后才会中断

编辑:你没有解决索引偏移问题。看看这个基本循环

int minIndex=0;
int maxIndex=data.Length-1;

while (minIndex <=maxIndex) { //this is the only control you have to do
  int mid = (minIndex + maxIndex) / 2;
  if (key == data[mid]) { //compare, or do stuff
     return ++mid;
  } else if (key < data[mid]) { //compare or do stuff
     maxIndex = mid - 1; //this -1 is important in performance
  }else {
     minIndex = mid + 1; //this +1 is important in performance
  }
}
int minIndex=0;
int maxIndex=data.Length-1;

虽然(minIndex找出哪一行代码抛出异常,哪一个索引变量超出边界,以及它是如何填充的。您可能没有正确地执行边界检查。我的意思是,while循环本身就是抛出异常的原因。请在每次迭代后检查
middle
的值,以确保它不超过boun数组第一维的ds。刚发现当数组的最大大小仅设置为10时,Middle返回的值为19,我尝试将最大值更改为20,但在搜索时它返回的值为39,这部分显然有些怪异,但不太确定是什么。@Sheevpaptine阅读了我的答案。你可以做很多改进欢迎来到这里。