| Author |
default size of arraylist
|
kiran kumar
Greenhorn
Joined: May 24, 2008
Posts: 18
|
|
Hi, what is the default size of arraylist?
|
Thanks & Regards,<br />Bhuvan Samrat.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
the default size is 1
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
No its not. Try running this: and see what you get.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Hi paul, yes.you are right.the answare is 1 .but when you add one element to an ArrayList..it will allocate dynamically size 1 right? please..tell me the default size of vector, hashtable and hashMap
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by seetharaman venkatasamy: Hi paul, yes.you are right.the answare is 1
sorry answare is 0(wrongly written)
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
but when you add one element to an ArrayList..it will allocate dynamically size 1 right?
It depends on how you add an element. If you use the method add(Object) of the type List the list will be resized. If you use add(int, Object) you will get an IndexOutOfBoundsException.
please..tell me the default size of vector, hashtable and hashMap
You can easily adapt the above program to find this out.
|
 |
Christoph Naber
Greenhorn
Joined: Jun 09, 2008
Posts: 1
|
|
Are you sure the OP didnt mean "initial capacity"? If so, this would default to 10, whereas the "default size" would always be 0 unless the - Constructor is used. Then the size would be (obviously) c.size(). Hava look: http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html Prost Christoph [ June 09, 2008: Message edited by: Christoph Naber ] [ June 09, 2008: Message edited by: Christoph Naber ]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Originally posted by Paul Sturrock: It depends on how you add an element. If you use the method add(Object) of the type List the list will be resized. If you use add(int, Object) you will get an IndexOutOfBoundsException.
Only if the index is not 0 (or more formal, if the index is out of range (index < 0 || index > size()))
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: default size of arraylist
|
|
|