简体   繁体   English

如果数组不是降序还是升序,为什么我的代码不会返回false?

[英]why will my code not return false if the array is not descending or ascending order?

public class SmallestLargest {

    public static void main(String[] args) {
        int [] arr = {3,6,2,1,23,6,7,54,3,2};
        System.out.println(isIncreasing(arr));

    }
    public static boolean isIncreasing(int[]arr)
    {
        for(int i=1; i<arr.length;i++)
        {
            if(arr[i] > arr[i+1] || arr[i] < arr[i+1] )
                return true;
        }
        return false;
     }

    }

so if the array is int[] arr = {1,2,3} it should return true and if its descending order it should also be true anything else and it should return false, but my output is always true. 因此,如果数组为int [] arr = {1,2,3},则它应返回true,如果其降序也应为true,否则它应返回false,但我的输出始终为true。

If you will check if your Array is assending you have to Change your check: 如果要检查阵列是否在分配中,则必须更改检查:

public class SmallestLargest {

    public static void main(String[] args) {
        int [] arr = {3,6,2,1,23,6,7,54,3,2};
        System.out.println(isIncreasing(arr));

    }
    public static boolean isIncreasing(int[]arr)
    {
        for(int i=0; i<arr.length-1;i++)
        {
            if( arr[i] > arr[i+1] )
                return false;
        }
        return true;
     }

    }

And your loop must end at arr.length-1 If not you get an ArrayIndexOutOfBoundsException . 并且您的循环必须at arr.length-1结尾at arr.length-1您将获得ArrayIndexOutOfBoundsException

And you have to start at 0, because Arrays in Java are Zero based. 而且您必须从0开始,因为Java中的数组基于零。

The problem is as long as you have one value that is smaller than the other your code will return true. 问题是,只要您拥有一个小于另一个值的值,您的代码就会返回true。 Your code should look like this. 您的代码应如下所示。

public class SmallestLargest {

public static void main(String[] args) {
    int [] arr = {3,6,2,1,23,6,7,54,3,2};
    System.out.println(isIncreasing(arr));

}
public static boolean isIncreasing(int[]arr)
{
    for(int i=0; i<arr.length - 1;i++)
    {
        if(arr[i] > arr[i+1]){
            return false;
        }
    }
    return true;
 }

}

This will return true if the array is sorted - either ascending or descending. 如果数组已排序-升序或降序,则返回true。 It can also handle flat sections of the array, where all entries are flat, one entry and no entries. 它还可以处理数组的平坦部分,其中所有条目都是平坦的,一个条目却没有条目。

It is debatable whether an empty array is sorted. 是否对空数组排序是有争议的。

public enum Direction {

    Descending {

                @Override
                boolean is(int a, int b) {
                    return b <= a;
                }
            },
    Ascending {
                @Override
                boolean is(int a, int b) {
                    return a <= b;
                }
            };

    abstract boolean is(int a, int b);
}

public static boolean isSorted(int[] n) {
    // All possible directions.
    Set<Direction> possibles = EnumSet.<Direction>allOf(Direction.class);
    for (int i = 0; i < n.length - 1 && possibles.size() > 0; i++) {
        // Check all currently possible directions.
        for (Iterator<Direction> it = possibles.iterator(); it.hasNext();) {
            Direction d = it.next();
            if (!d.is(n[i], n[i + 1])) {
                // Not in that direction.
                it.remove();
            }
        }
    }
    return possibles.size() > 0;
}

public void test(int[] arr) {
    System.out.println(Arrays.toString(arr) + " is " + (!isSorted(arr) ? "not " : "") + "sorted");
}

public void test() {
    test(new int[]{3, 6, 2, 1, 23, 6, 7, 54, 3, 2});
    test(new int[]{3, 4, 5, 6, 7, 8, 9});
    test(new int[]{3, 4, 4, 4, 5, 6, 7, 9});
    test(new int[]{4, 4, 4, 4});
    test(new int[]{1});
    test(new int[]{});
    test(new int[]{100, 50, 25, 12, 6, 3, 1});
}

Try this out: 试试看:

boolean status=false;
public static boolean isIncreasing(int[]arr)
{
    for(int i=0; i<arr.length-1;i++)
    {
        if(arr[i] > arr[i+1] || arr[i] < arr[i+1] )
            status=true;
        else
            return false;
    }
    return true;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM