Any help? Please?

I would like to know- initialising an ArrayList with a default size, eg, ArrayList list =new ArrayList<Object>(30);

will this create an ArrayList with 30 null values?

Also, would invoking set(int index, Object element);

result in an IndexOutOfBoundsException if done on the ArrayList as initialised above? If yes, what should be done instead?

Any help would be appreciated... Thanks

'Segun

[501 byte] By [Oluseguna] at [2007-10-2 20:44:15]
# 1

> I would like to know- initialising an ArrayList with

> a default size, eg, ArrayList list = new

> ArrayList<Object>(30);

will this create an

> ArrayList with 30 null values?

It will create an ArrayList that doesn't have any values.

The underlying array will have 30 null values.

> Also, would invoking set(int index, Object

> element);

> result in an IndexOutOfBoundsException if done on the

> ArrayList as initialised above?

Try it.

> If yes, what should

> be done instead?

Depends. What are you trying to accomplish? Putting something in position 20 in an ArrayList that has fewer than 20 elements is meaningless.

jverda at 2007-7-13 23:27:41 > top of Java-index,Java Essentials,Java Programming...
# 2
You only need to specify the initial size of array list.ArrayList list = new ArrayList (30);
masuda1967a at 2007-7-13 23:27:41 > top of Java-index,Java Essentials,Java Programming...
# 3
> Try it.Thanks for the info... I tried it, the exception got thrown.I'm trying to set the list contents to particular values, one by one, in a for loop.'Segun
Oluseguna at 2007-7-13 23:27:41 > top of Java-index,Java Essentials,Java Programming...
# 4
// Can you just create and add?ArrayList list = new ArrayList ();// your for conditionfor (){list.add( yourObject ); }
masuda1967a at 2007-7-13 23:27:41 > top of Java-index,Java Essentials,Java Programming...