Can anybody explain why use of i>0 after && operator giving index out of bound exception while using it before isn't

Viewed 48

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;
    

    `
1 Answers

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.

Related