java cmp是什么_Java程序问题,求大神解答: 参数cmp是哪里来的,代码帖下面了

publicclassMain{publicstaticvoidmain(String[]args){Bar[]bars={newBar(12),newBar(23),newBar(34),newBar(45),newBar(56),newBar(67),newBar(78),newBar(89)};System.out.println(...

public class Main {

public static void main(String[] args) {

Bar[] bars = {new Bar(12), new Bar(23), new Bar(34), new Bar(45),

new Bar(56), new Bar(67), new Bar(78), new Bar(89)};

System.out.println(bsearch(bars, new Bar(12)));

System.out.println(ssearch(bars, new Bar(12)));

}

public static > int bsearch(T[] array, T key) {

return bsearch(array, key, 0, array.length-1);

}

public static > int bsearch(T[] array, T key, int left, int right) {

int cmp, middle = right / 2;

if (left > right) {

return -1;

}

else if ((cmp = key.compareTo(array[middle])) == 0) {

return middle;

}

else if (cmp < 0) {

return bsearch(array, key, left, middle-1);

}

else {

return bsearch(array, key, middle+1, right);

}

}

public static > int ssearch(T[] array, T key) {

return ssearch(array, key, 0);

}

private static > int ssearch(T[] array, T key, int index) {

if (index >= array.length) {

return -1;

}

else if (array[index].compareTo(key) == 0) {

return index;

}

return ssearch(array, key, index+1);

}

}

class Foo implements Comparable {

public Foo(int foo) {

this.foo = foo;

}

public int compareTo(Foo other) {

return foo - other.foo;

}

public String toString() {

return Integer.toString(foo);

}

private int foo;

}

class Bar extends Foo {

public Bar(int bar) {

super(bar);

this.bar = bar;

}

private int bar;

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值