怎么ArrayList中 BinarySearch()方法,搜索出来的索引为负数(怎么会小于零)呀?

BigIdiot628 2005-12-02 05:30:44
前提是,值确实存在于 ArrayList 中。

但并不是搜索所有的对象,得到的索引都小于零。
有时正确,有时不正确。
...全文
273 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BigIdiot628 2005-12-04
  • 打赏
  • 举报
回复
foreach(DataRow dr in myDataSet.Tables["Order"].Rows )
{
myAL.Add(dr["OrderID"]);
}
int orderID = myDataSet.Tables["Order"].Rows [0]["OrderID"] //orderID 是随机取表中的OrderID,OrderID 为主键,加入到ArrayList中。
但搜索时,用:
myAL.BinarySearch(orderID );或
myAL.IndexOf(orderID );时,有时候搜索得到索引,有时候则返回为一个小于零的索引。
Macosx 2005-12-02
  • 打赏
  • 举报
回复
Return Value
The zero-based index of value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than value or, if there is no larger element, the bitwise complement of Count.

看一下MSDN不就好了
BigIdiot628 2005-12-02
  • 打赏
  • 举报
回复
好。
我测试时,是把一个表中的ID(PrimaryKey)加入到 ArrayList中。
再通过此表的ID来搜索ArrayList,但发现有些索引小于0,有些就返回正确。

可能是查找失败。
mbh0210 2005-12-02
  • 打赏
  • 举报
回复
你搜索的在ArrayList中不存在。。。
李必胜 2005-12-02
  • 打赏
  • 举报
回复
代码呢
jxufewbt 2005-12-02
  • 打赏
  • 举报
回复
应该是没有找到
netzww 2005-12-02
  • 打赏
  • 举报
回复
没有找到,象IndexOf()等如果没有找到就返回-1
jinjazz 2005-12-02
  • 打赏
  • 举报
回复
没找到吧

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧