2013-10-18 80 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

相关问题