• Post Reply Bookmark Topic Watch Topic
  • New Topic

arraylist?  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why the above code throws exception?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the API for the add method in ArrayList: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(E)
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tell me the difference between two add statements in above program?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference except first one adds 12 and second one adds 56.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would also find out useful information from the stack trace from the Exception. Even looking at the Exception's name will help.
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void add(int index,
E element)

IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

when you say a.add(2,3); your size = 1 and index =2
 
dhrubo bhattacharjee
Greenhorn
Posts: 23
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By going through the ArrayList.add Documentation it says that an IndexOutOfBoundsException is thrown if :
Check the size() of your list before you call to find if the position you are trying to add is less than the size of the list, if not then you can just do a normal
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!