-2

enter image description here > this is giing arrayIndexOutOfBound Exception

  **       for(int i=0; i< arr.length; i++){
                    start =0;
                    if((arr[i]==arr[i-1]) && i>0){
                        start =  end+1;
                    }
                      end = outer.size()-1;**    
> This is working

                `for(int i=0; i< arr.length; i++){
                    start =0;
                    if(i>0 && (arr[i]==arr[i-1]) ){
                        start =  end+1;
                    }
                    end = outer.size()-1;
    

    `
  • 2
    arr[i-1] -> what do you think this results in if i == 0 ? -1 is an 'index out of bounds' – Stultuske Sep 19 '22 at 05:52
  • 1
    Does this answer your question? [&& (AND) and || (OR) in IF statements](https://stackoverflow.com/questions/1795808/and-and-or-in-if-statements) – wake-0 Sep 19 '22 at 06:01
  • 1
    Remember that SO isn't an "anything goes" general help forum, you are expected to follow the same posting guidelines as everyone else: don't link to pictures of text, [put the text in your post](/help/how-to-ask). And of course, follow the rest of those posting guidelines, and hit the [edit] button because that code block is not all code. – Mike 'Pomax' Kamermans Sep 19 '22 at 06:08
  • The order of evaluation matters due to “short-circuiting”. – pjs Sep 19 '22 at 17:20

1 Answers1

0

Because on the first one, you're doing

    (arr[i]==arr[i-1])

before making sure i > 0. That part arr[i-1] become arr[-1] when i is 0.

Milford P
  • 48
  • 6