• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt from Wildcard in generics : Code throwing IndexOutOfBoundsException

 
Greenhorn
Posts: 8
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please suggest me why does the below code throws a IndexOutOfBoundsException :


Output :
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has nothing to do with generics. The error message is telling you exactly what's wrong.

At line 10 of WildcardDemo1.java, you're trying to access the first element (at index 0) of an ArrayList that has no elements.

Go back and read the docs for set() very closely.

And after you've done that, and understand why you're getting the error, change your code to not use set() or get() at all. You should never use get() when iterating a List, and you can just use add() instead of set().



But you don't even need to do that:


The only caveat to these is that if you want dst to exactly match src when you're done, you'll need to call dst.clear() first. And if you want dst to keep its original size but have the same elements as src for whichever indices they both have in common, then you'll need to use set(), but you'll need to alter your approach so that you're not trying to put values in elements that don't exist.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:
But you don't even need to do that:



Jeff is right. You don't have to write for loop and then set..

Jeff Verdegan wrote: At line 10 of WildcardDemo1.java, you're trying to access the first element (at index 0) of an ArrayList that has no elements.



But Jeff, If the ArrayList does not contain elements then there is no chance of getting into for loop which is the cause of the IndexOutOfBoundsException, right?

Thanks
BVR.
 
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the ArrayList does not contain elements then there is no chance of getting into for loop which is the cause of the IndexOutOfBoundsException, right?



You mean to say an enhanced for loop would throw an exception if the list is empty? No. it wouldn't. It just won't enter the loop. The reason for the IOOB is because list.set(i, obj) accesses an index in list which will not be there on an empty list, which is what Jeff had mentioned and that's why you'd use add()/addAll() instead of set() when you want to add an element to an empty list.

Please not Line 10 is inside the loop and not the beginning of the loop.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Bhunia wrote:Can anybody please suggest me why does the below code throws a IndexOutOfBoundsException :


I believe the others have covered it for you; however, it may be worth pointing out that you may still have problems when you get the code corrected. Modification methods don't generally work well with wildcards (I suggest you read the Generics tutorial for an explanation why); and in your case the wildcards aren't even the same.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:

Jeff Verdegan wrote: At line 10 of WildcardDemo1.java, you're trying to access the first element (at index 0) of an ArrayList that has no elements.



But Jeff, If the ArrayList does not contain elements then there is no chance of getting into for loop which is the cause of the IndexOutOfBoundsException, right?





If src has no elements, then we will not get into the loop. But I didn't say that src has no elements. I also suggested reading the docs for set() more closely.
 
mooooooo ..... tiny ad ....
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic