Ester
Ester

Reputation: 133

Check if successive Elements are different (less than)

I am wondering if there is a way to compare successive elements in an arrayList. I have this

for (int j=0; j< Index.size(); j++) {
   if(Index.get(j) < Index.get(j -1) {  
       System.out.println("Total number of shapes is " + sizer);
   } 
}

Problem is my code crashes when it gets to this point and I am not sure how to fix it.

Thanks in advance

Upvotes: 0

Views: 192

Answers (2)

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

Change the start of the for loop:

for (int j=1; j< Index.size(); j++) {
   if(Index.get(j) < Index.get(j -1) {  
       System.out.println("Total number of shapes is " + sizer);
   } 
}

Upvotes: 1

shyam
shyam

Reputation: 9368

your index starts at 0 and you are trying to get the -1th element instead try initializing j to 1

Upvotes: 4

Related Questions