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
}