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


This Content is from Stack Overflow. Question asked by Aiyan Faras

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;



Because on the first one, you’re doing


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

This Question was asked in StackOverflow by Aiyan Faras and Answered by Milford P It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?