Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Index -1 out of bounds

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Index -1 out of bounds

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Index -1 out of bounds

    Yes, 0 - 1 = -1

    How did you fix the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Index out of bounds exception. Need help.
    By chernorizec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 13th, 2014, 05:03 PM
  2. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  3. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  4. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM

Tags for this Thread