2015-04-01 57 views
0

我想要完成此合併排序方法,但出於某種原因,我顯示錯誤「運算符< =未定義參數類型(s) T,T」運算符<=未定義爲參數類型

的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 
+0

請在您的問題上添加語言標籤(Java?)。並縮進代碼,使其更具可讀性。 – Barmar 2015-04-01 21:38:40

+0

在我的教科書中的另一個問題是,有一行代碼表示「交換一個[indexFromLeft]和一個[indexFromRight]」我知道這個「並且」意味着&&但是「Exchange」是什麼意思? – ANIME4154142 2015-04-01 22:13:17

+0

*但「Exchange」是什麼意思?* - 開關?你確定'和'的意思是'&&'在那一行嗎?因爲'&&'通常被定義爲'AND'。 – WonderWorld 2015-04-01 22:19:46

回答

1

運算符> = <是基本類型。你應該使用compareTo for T(如果它是可比較的)。

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

我的教科書中的另一個問題是有一行代碼表示「交換一個[indexFromLeft]和一個[indexFromRight]」我知道這個「和」意味着&&但是「Exchange」是什麼意思? – ANIME4154142 2015-04-01 22:10:52

相關問題