【问题标题】:The operator <= is undefined for the argument type(s)运算符 <= 未定义参数类型
【发布时间】:2015-04-01 21:36:35
【问题描述】:

我正在尝试完成此合并排序方法,但由于某种原因,我看到一个错误“操作符

对于 if 语句

if(a[beginHalf1] <= a[beginHalf2])
      {

        tempArray[index] = a[beginHalf1];
        beginHalf1++;
      }

这是完整的代码

public static <T extends Comparable<? super T>>
    void merge(T[] a, T[] tempArray, int first, int mid, int last){


           int beginHalf1 = first;
           int endHalf1 = mid;
           int beginHalf2 = mid +1;
           int endHalf2 = last;
           int index = 0;

    while((beginHalf1 <= endHalf1) && (beginHalf2 <= endHalf2) )
          {

           if(a[beginHalf1] <= a[beginHalf2]){

           tempArray[index] = a[beginHalf1];
           beginHalf1++;
        }

        else
        {
            tempArray[index]=a[beginHalf2];
            beginHalf2++;
        }
     index++;
    }




    } // end merge

【问题讨论】:

  • 请在您的问题中添加语言标签(Java?)。并缩进代码,使其更具可读性。
  • 我教科书中的另一个问题有一行代码说“交换a[indexFromLeft]和a[indexFromRight]”我知道“and”的意思是&&但是“Exchange”是什么意思?
  • 但是“交换”是什么意思? - 切换?您确定and 在该行中表示&amp;&amp; 吗?因为&amp;&amp; 通常定义为AND
  • “交换”是指互相替换。这里的“and”只是正常的英文意思,与&amp;&amp;的逻辑运算无关。

标签: java sorting if-statement merge


【解决方案1】:

运算符 >=

if(a[beginHalf1].compareTo(a[beginHalf2]) <= 0)

【讨论】:

  • 我教科书中的另一个问题有一行代码说“交换a[indexFromLeft]和a[indexFromRight]”我知道“and”的意思是&&但是“Exchange”是什么意思?
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 2018-06-26
  • 2018-06-30
  • 2018-04-10
  • 2021-07-03
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多