A silly question about ArrayList.add() method
I have a question regarding
java.util.ArrayList.add(int index, Object element);
publicclass Test{
publicstaticvoid main(String args[]){
ArrayList<String> aList =new ArrayList<String>();
aList.add(1,"element2");
aList.add(0,"element1");
aList.add(2,"element3");
for(int i=0; i<aList.size(); i++){
System.out.println("Element" + (i+1) +": " + aList.get(i));
}
}
}
When I run the program as a Java Application I am getting following error:
Exception in thread"main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(Unknown Source)
at Test.main(Test.java:4)
If we are not able add an element to ArrayList at the random position
what is the purpose of having two add methods in Arraylist?
add(int index, Object element)
add(Object o)
Can any one please tell me, are we able to add an element to Arraylist (list in general) at index position we want irrespective of index and number of elements. Is there a way we can add element at Index 1 first and add another element at Index 0 later?
Thanks in advance.>
[1811 byte] By [
rsreddycha] at [2007-10-3 4:05:41]

Hi jverd,
Thanks for replying so fast.
I am still getting the same error when I run the following code:
public class Test2 {
public static void main(String[] args) {
ArrayList<String> aList = new ArrayList<String>(3);
aList.add(1, "element2");
aList.add(0, "element1");
aList.add(2, "element3");
for(int i=0; i<aList.size(); i++){
System.out.println("Element" + (i+1) + ": " + aList.get(i));
}
}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(Unknown Source)
at Test2.main(Test2.java:6)
Am I doing something wrong. Can you put some sample code please?
Thanks,
rsreddych>
aList.add(1, "element2");
You can't add at position 1 yet. That's the second slot. The first element you add will always go into position 0. Using new ArrayList(3) to construct it doesn't give it three elements. It just gives it an initial capacity of 3. That just means that the backing array has 3 slots you can use, and if you need to add more than 3 elements, it will have to create a new, larger array.
jverda at 2007-7-14 22:05:02 >

Hey jverd,
Thanks once again.
My question is, can I add an element at position 1 fisrt and add an element at position 0 later?
Here is what I mean to say:
Can I add element 2 to ArrayList first at position at index 1 as in the following line?
aList.add(1, "element2");
Are we able to add the element later at Index 0 as in the following line?
aList.add(0, "element1");
If we can't achieve that do you know the purpose of having following two methods:
add(int index, Object element)
add(Object o)
since the method
add(Object o)
adds the next element at the end of the list.?
Thanks for your patience.
> Hey jverd,
>
> Thanks once again.
>
> My question is, can I add an element at position 1
> fisrt and add an element at position 0 later?
No. This is what I'm telling you.
The first element will always go into position 0.
The second can go into 0 or 1.
Third can go into 0, 1, or 2.
etc.
> If we can't achieve that do you know the purpose of
> having following two methods:
After you have N elements in the list, you can stuff something in other than at the end--at positions zero through N (where add(N, obj) is the same as add(obj).
I've never used it though. I always just add at the end.
jverda at 2007-7-14 22:05:02 >
