Can someone explain that error? I do not get what they mean by saying "Index -1", I don't have such an index in my code ??
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 17 at LengthSequence.main(LengthSequence.java:16)
from this code
int[] numbers = { 1, 1, 2, 3, 3, 4, 5, 2, 4, 5, 6, 7, 8, 9, 6, -1, -2 }; System.out.println("Original array: " + Arrays.toString(numbers)); int count = 0; ArrayList<Integer> consLength = new ArrayList<Integer>(); for (int i = 0; i < numbers.length; i++) { System.out.println("Index i : " + i); if (numbers[i - 1] <= numbers[i]) { count++; } else { consLength.add(count); count = 0; } } System.out.println(Collections.max(consLength) + 1);
--- Update ---
I got it. Since we compare i-1 and 1. That's why.