Can the index number be specified in the ArrayList add method
Varuna Seneviratna
Ranch Hand
Joined: Jan 15, 2007
Posts: 164
posted
0
Can the index number be specified in the ArrayList add method If the first elemnt to be added is four
4 specified as the index number, which should place four in the forth place in the il ArrayList.Is this posible?
The below program uses a feature like that
When I specified four as the index number of zero the first addition to the list the message was Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 0 at java.util.ArrayList.add(ArrayList.java:367) at ArrayListMagnet.main(ArrayListMagnet.java:10)
If the index number can be specified when an element is added, what is the use if the index number can not be a choice of the programmer?
Varuna
Varuna Seneviratna
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
You can only specify an index that is less than the number of elements already in the ArrayList, so you can only use 4 if you already have four or more elements in the list. [ October 02, 2008: Message edited by: Joanne Neal ]