Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and IndexOutOfBoundsException

 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to execute the the Program

I got IndexOutOfBoundsException .
Does this means that the dynamic growing of the ArrayList doesnot happen when we add an element using indexes?

When I reexecuted with the initial capacity =10 ,got the same exception.

So what initialCapacity is doing in the constructor of ArrayList?
[ December 17, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjeev,

I works when you say 4, "E" instead of 5. Any reason behind this behavior to throw an exception??
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code below will help you.

This is the add method as defined,



Hope, now you can see why your code throws the exception. Hope this helped.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic