• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic