• Post Reply Bookmark Topic Watch Topic
  • New Topic

Index out of bound exception while comparing elements in the same list dynamically  RSS feed

 
sup rty
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am adding some values dynamically to an arraylist and comparing the values stored in the list with the previous values.mWhlw doing this I am getting the following exception "java.lang.IndexOutOfBoundsException: Index: 2, Size: 2".
Hers is the code snippet:


Kindly look into this issue and please let me know if anyone has any idea.

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using "i <= dest.size()", then "dest.get(i)". This will cause an exception, because a list index starts at 0 and finishes at size()-1 included.
 
sup rty
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Christophe for the info.
So If i do some thing like this will it work :

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, although I'm not sure your logic is correct.
1. You are comparing only adjacent object. Are you sure this is what you want ? I mean, if there are 3 objects, the object at index 0 won't be compared to the object at index 2.
2. You're not comparing strings, are you ? Otherwise using "==" would be a bad idea.
 
sup rty
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe,

I actually want to compare all the previous elements. not the adjacent elements. I guess My logic is wrong. could you please tell me how could change my code to compare element in a list with all previous element.
for eg.i f there are 3 objects,how can i compare the object at index 0 to the object at index 2.

Yes i am comparing two strings, so i will use equals method for the same.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compare an object in a list with all other objects in the same list, you must use a nested for-loop within your existing for-loop - the inner for-loop defined the same way as the outer(for 1 to size-1 step 1).
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...., whats the objective of this piece of code? - like, you want to detect when a duplicate String is being added to your arraylist or something?
I'm not sure what your requirement is, but...but if so, you can go for a Set instead of an ArrayList.
 
sup rty
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The main objective of my code is to compare the elements in the list, and if the same element exists in the list then do some action else do some other action. could you please share some code snippet that could help me.


thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!