(Java) Check array for increasing elements

前端 未结 5 1681
旧时难觅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条回答
  •  孤街浪徒
    2021-01-03 14:17

    You can use Java 8's IntStream.

    import java.util.stream.IntStream;
    
    public class Test {
      public static boolean isIncreasing(int[] a) {
        return IntStream.range(1, a.length).reduce(0, (acc, e) -> acc + (a[e - 1] <= a[e] ? 0 : 1)) == 0;
      }
    }
    

提交回复
热议问题