(Java) Check array for increasing elements

前端 未结 5 1703
旧时难觅i
旧时难觅i 2021-01-03 13:48

I\'m attempting to create a method that checks an array for increasing elements. True should be returned if all the elements are in increasing order. I get an out-of-bounds

5条回答
  •  旧时难觅i
    2021-01-03 14:10

    I suggest you write your method like this

    public static boolean isIncreasing(int[]arr)
    {
        for(int i=1; iarr[i])
                return false;
        }
        return true;
     }
    

    it will help

    • return the proper result (yours returns true when it should not)
    • consider out of bounds
    • avoid unnecessary looping

提交回复
热议问题