| Author |
Doubt from Wildcard in generics : Code throwing IndexOutOfBoundsException
|
Raj Bhunia
Greenhorn
Joined: Apr 10, 2011
Posts: 8
|
|
Can anybody please suggest me why does the below code throws a IndexOutOfBoundsException :
Output :
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5893
|
|
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.
|
 |
vipul bondugula
Ranch Hand
Joined: Oct 14, 2010
Posts: 218
|
|
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.
|
 |
Prabaharan Gopalan
Ranch Hand
Joined: Oct 16, 2009
Posts: 66
|
|
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.
|
Googling doesn't make you a genius. But not Googling makes you dumber.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4761
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5893
|
|
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.
|
 |
 |
|
|
subject: Doubt from Wildcard in generics : Code throwing IndexOutOfBoundsException
|
|
|