Arrays.binarySearch的插入点问题

theoffspring 2010-05-17 09:39:26


String[] colors =
{"blue", "red", "green", "yellow", "orange"};
Arrays.sort(colors);//此时数组为blue,green,orange,red,yellow
int s2 = Arrays.binarySearch(colors, "orange");
int s3 = Arrays.binarySearch(colors, "violet");
System.out.print(s2 + "" + s3);


s3应该等于(-插入点-1),插入点应该是3,输出结果应该是2-4,但为什么输出是2-5,也就是说插入点是4?奇怪了呀。
...全文
241 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
theoffspring 2010-05-18
  • 打赏
  • 举报
回复
我不是算错了就是在纸上少写了一个,不好意思
liuxiaolei7676 2010-05-18
  • 打赏
  • 举报
回复
楼主你怎么得出的插入点是3啊
插入点 被定义为将键插入数组的那一点:即第一个大于此键的元素索引
blue,green,orange,red,yellow
大于violet值的第一个元素应该是yellow 索引是4 返回 -4-1=-5
shine333 2010-05-17
  • 打赏
  • 举报
回复
lz,你怎么算的
blue,green,orange,red,violet,yellow

insertion point = 4 (zero-based)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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