检查数组是否已排序,返回 true 或 false [英] Check if an array is sorted, return true or false

查看:45
本文介绍了检查数组是否已排序,返回 true 或 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的程序,如果数组已排序,则返回 true,否则返回 false,并且我在 eclipse 中不断收到异常,但我不知道为什么.我想知道是否有人可以看看我的代码并解释为什么我得到一个数组越界异常.

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 wondering if someone could take a look at my code and kind of explain why I'm getting an array out of bounds exception.

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));   
}

推荐答案

让我们看一下您构建的循环的更简洁版本:

Let's look at a cleaner version of the loop you constructed:

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

我应该首先指出原始循环中的语法错误.即,在开始循环体的花括号 ({) 之前有一个分号 (;).应该去掉那个分号.另请注意,我重新格式化了代码的空格以使其更具可读性.

I should first point out the syntax error in the original loop. Namely, there is a semicolon (;) before the curly brace ({) that starts the body of the loop. That semicolon should be removed. Also note that I reformatted the white-space of the code to make it more readable.

现在让我们讨论循环内部发生的事情.循环迭代器 i0 开始,到 a.length - 1 结束.由于 i 用作数组的索引,因此指出 a[0] 是第一个元素和 a[a.length - 1] 是有道理的 数组的最后一个元素.但是,在循环体中,您还编写了 i + 1 的索引.这意味着如果 i 等于 a.length - 1,则您的索引等于 a.length 超出了数组.

Now let's discuss what happens inside your loop. The loop iterator i starts at 0 and ends at a.length - 1. Since i functions as an index of your array, it makes sense pointing out that a[0] is the first element and a[a.length - 1] the last element of your array. However, in the body of your loop you have written an index of i + 1 as well. This means that if i is equal to a.length - 1, your index is equal to a.length which is outside of the bounds of the array.

函数 isSorted 也有相当多的问题,因为它在第一次 a[i] a[i] <;a[i+1] 和 false 第一次不是;因此,它实际上根本不检查数组是否已排序!相反,它只检查前两个条目是否已排序.

The function isSorted also has considerable problems as it returns true the first time a[i] < a[i+1] and false the first time it isn't; ergo it does not actually check if the array is sorted at all! Rather, it only checks if the first two entries are sorted.

具有类似逻辑但检查数组是否确实已排序的函数是

A function with similar logic but which checks if the array really is sorted is

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.
}

这篇关于检查数组是否已排序,返回 true 或 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆