Check if an array is sorted, return true or false

前端 未结 13 2004
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 06:03

I am writing an easy program the just returns true if an array is sorted else false and I keep getting an exception in eclipse and I just can\'t figure out why. I was wonder

13条回答
  •  孤街浪徒
    2020-11-30 06:55

    With this expression, a[i+1], you are running off the end of the array.

    If you must compare to the next element, then stop your iteration 1 element early (and eliminate the semicolon, which Java would interpret as your for loop body):

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

提交回复
热议问题