2013-10-18 77 views
5

我正在寫一個簡單的程序,剛剛返回true,如果一個數組排序,否則爲false,我不斷地在eclipse中得到一個異常,我只是不知道爲什麼。我想知道是否有人可以看看我的代碼,並解釋爲什麼我得到一個數組越界異常。感謝您的高級幫助。檢查一個數組是否排序,返回true或false

public static boolean isSorted(int[] a) 
{ 

    int i; 
    for(i = 0; i < a.length; i ++);{ 
     if (a[i] < a[i+1]) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 
public static void main(String[] args) 
{ 
      int ar[] = {3,5,6,7}; 
      System.out.println(isSorted(ar)); 
} 
+3

通過您的代碼發佈異常 – Cruncher

+0

運行。你有4個條目,它應該很簡單。 「我」在某些時候將等於3,[3 + 1]會嘗試訪問什麼? – cklab

+1

檢查你的索引範圍 – 2013-10-18 20:15:08

回答

23

讓我們來看看您構建的循環的清潔版本:

for (i = 0; i < a.length; i++); { 
    if (a[i] < a[i + 1]) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

我應該先在原來的循環指出語法錯誤。也就是說,在啓動循環體的花括號({)之前有一個分號(;)。該分號應該被刪除。 另請注意,我重新格式化了代碼的空白區域,使其更具可讀性。

現在讓我們來討論你的循環內部會發生什麼。循環迭代器i開始於0並結束於a.length - 1。由於i作爲您陣列的索引,因此有意義的是指出a[0]是第一個元素,a[a.length - 1]是數組的最後一個元素。但是,在你的循環體中,你也寫了一個i + 1的索引。這意味着如果i等於a.length - 1,那麼您的索引等於a.length,它位於數組邊界之外。

功能isSorted也有相當大的問題,因爲它第一次返回true a[i] < a[i+1]並且第一次不成功時返回false; ergo它實際上並不檢查數組是否已排序!相反,它只檢查前兩個條目是否已排序。

有類似的邏輯功能,但它會檢查該數組真的排序是

public static boolean isSorted(int[] a) { 
// Our strategy will be to compare every element to its successor. 
// The array is considered unsorted 
// if a successor has a greater value than its predecessor. 
// If we reach the end of the loop without finding that the array is unsorted, 
// then it must be sorted instead. 

// Note that we are always comparing an element to its successor. 
// Because of this, we can end the loop after comparing 
// the second-last element to the last one. 
// This means the loop iterator will end as an index of the second-last 
// element of the array instead of the last one. 
    for (int i = 0; i < a.length - 1; i++) { 
     if (a[i] > a[i + 1]) { 
      return false; // It is proven that the array is not sorted. 
     } 
    } 

    return true; // If this part has been reached, the array must be sorted. 
} 
+0

感謝你的幫助,我明白現在 – user2101463

+0

@ user2101463很樂意幫忙 –

+0

最壞情況複雜度爲O(N)。考慮到數據是從正態分佈中隨機選擇的,平均時間複雜度是多少?它真的是O(1)嗎?看起來像一束遞減的幾何級數,其總和是另一個幾何級數,它只給出4 n個逼近n→∞,因此O(1)。這是真的? –

1

a[i+1]i == a.length會給你的錯誤。

例如,在長度爲10的陣列,則必須元素0至9

a[i+1]i爲9,將顯示a[10],這是出界。

要解決:只要回叫

for(i=0; i < a.length-1;i++) 

而且,您的代碼不通過整個陣列檢查,檢查循環被終止。 您只是檢查第一個值,並且只檢查第一個值。

AND,你有你的for循環聲明,這也導致問題

+0

AHHHH小得多!我現在看到了,謝謝你的幫助 – user2101463

2

有了這個表達,a[i+1],你正在運行關閉陣列結束後一個分號。

如果你必須比較到下一個元素,那麼提前停止你的迭代1元(並消除分號,這將Java的解釋爲你for循環體):

// stop one loop early ---v  v--- Remove semicolon here 
for(i = 0; i < a.length - 1; i ++){ 
0

你不應該使用a[i+1]因爲該值可能會或可能不會從陣列中消失。

例如:

A = {1, 2, 3} 
// A.length is 3. 
for(i = 0; i < a.length; i ++) // A goes up to 3, so A[i+1] = A[4] 

要解決這個問題,只需提前停止一環。

int i; 
for(i = 0; i < a.length - 1; i ++);{ 

    if (a[i] < a[i+1]) { 

     return true; 
    }else{ 
     return false; 

    } 

} 
+0

一個int數組不能爲null。同時,當你的代碼解決了這個異常時,它實際上並沒有達到該方法實現的目的。 –

+0

對不起,你是正確的。固定。 – dtgee

1

要檢查數組是否排序,我們可以比較數組中的相鄰元素。

檢查的null & a.length == 0

public static boolean isSorted(int[] a){  

    if(a == null) { 
     //Depends on what you have to return for null condition 
     return false; 
    } 
    else if(a.length == 0) { 
     return true; 
    } 
    //If we find any element which is greater then its next element we return false. 
    for (int i = 0; i < a.length-1; i++) { 
     if(a[i] > a[i+1]) { 
      return false; 
     }   
    } 
    //If array is finished processing then return true as all elements passed the test. 
    return true; 
} 
2
int i; 
for(i = 0; i < a.length - 1 && a[i] < a[i+1]; i++){} 
return (i == a.length - 1); 
  • 邊界條件僅訪問的數組元素中,結束條件最後一部分未 處理上第一不如果第一部分IST假
  • 停止排序元素
-3

Array.prototype.every

每()數組中的所有元素方法測試是否通過由提供的功能來實現的測試。

arr.every(function (a, b) { 
    return a > b; 
}); 

var arr = [1,2,3] // true 

var arr = [3,2,1] // false 
+0

注意到這是一個關於java的問題,但是查看了src。每個()都會有一些亮點;) – iamwhitebox

+0

這個回答是錯誤的,不僅因爲它適用於'js',而且因爲它沒有做它應該做的事情。 'every'函數當時只測試一個元素,所以在這種情況下,arg'b'就是元素'a'的索引。所以提供的函數只測試'arr [i]> i'。對於'[5,3,5]'它返回'true',而對於'[0,1,2]''則返回'false'。 –

0

降序數組也被排序。考慮到這兩種升序和降序陣列,我使用下面的:

public static boolean isSorted(int[] a){ 
    boolean isSorted = true; 
    boolean isAscending = a[1] > a[0]; 
    if(isAscending) { 
     for (int i = 0; i < a.length-1; i++) { 
      if(a[i] > a[i+1]) { 
       isSorted = false; 
       break; 
      }   
     } 
    } else {//descending 
     for (int i = 0; i < a.length-1; i++) { 
      if(a[i] < a[i+1]) { 
       isSorted = false; 
       break; 
      }   
     } 
    }  
    return isSorted; 
} 
0
public static boolean isSorted(int[] a) 
{ 
    for (int i = 0; i < a.length - 1 ; i++) { 
     if (a[i] > a[i+1]) 
      return false; 
    } 
    return true; 
} 

此功能檢查陣列是在升序或沒有。

+1

你能解釋你的答案嗎? –

+3

歡迎來到Stack Overflow!儘管這段代碼可能會解決這個問題,其中包括* how *和* why *的解釋,這可以解決問題[真的有所幫助](// meta.stackexchange.com/q/114762)來提高帖子的質量。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –

+0

歡迎來到堆棧溢出:-) 請看[答]。您應該提供一些信息,說明爲什麼您的代碼可以解決問題。 僅有代碼的答案對社區沒有用處。 – JimHawkins

相關問題