Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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中的arraylist# 有没有任何可能的方法使代码更短?_C#_Winforms_Binary Search - Fatal编程技术网

C# 将二进制搜索解析为C中的arraylist# 有没有任何可能的方法使代码更短?

C# 将二进制搜索解析为C中的arraylist# 有没有任何可能的方法使代码更短?,c#,winforms,binary-search,C#,Winforms,Binary Search,您似乎已经将事情严重地过度复杂化了,您当然不应该将数字转换为字符串,而只是将其解析回数字 int index = ListOfPeople.BinarySearch(searchBar.Text); 当找不到项目时,这将返回一个非负数。它不返回int?,而是返回int 因此,现在您可以使用它: int index = ListOfPeople.BinarySearch(searchBar.Text); if (index >= 0) { MyPeopleGrid.Selectio

您似乎已经将事情严重地过度复杂化了,您当然不应该将数字转换为字符串,而只是将其解析回数字

int index = ListOfPeople.BinarySearch(searchBar.Text);
当找不到项目时,这将返回一个非负数。它不返回
int?
,而是返回
int

因此,现在您可以使用它:

int index = ListOfPeople.BinarySearch(searchBar.Text);
if (index >= 0)
{
    MyPeopleGrid.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
    MyPeopleGrid.Rows[index].Selected = true;
    MyPeopleGrid.CurrentCell = MyPeopleGrid.Rows[index].Cells[0];
}
else
{
    // do something when the item isn't found
}

我需要那个int I,否则这段代码不工作
gdv.CurrentCell=gdv.Rows[I].Cells[0]好的,那么你到底想做什么?为什么需要一个可为null的int?这是干什么用的?在用BinarySeach搜索之后,它选择了行。我发布的代码运行得很好,但是我很好奇是否有一个更简短的方法来编写代码。你能发布更多的代码吗?我不明白为什么您不只是使用
ArrayList.BinarySearch(searchBar.Text)没有额外的东西。
int index = ListOfPeople.BinarySearch(searchBar.Text);
if (index >= 0)
{
    MyPeopleGrid.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
    MyPeopleGrid.Rows[index].Selected = true;
    MyPeopleGrid.CurrentCell = MyPeopleGrid.Rows[index].Cells[0];
}
else
{
    // do something when the item isn't found
}