• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default size of arraylist

 
kiran kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
what is the default size of arraylist?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the default size is 1
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its not. Try running this:

and see what you get.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:
Hi paul,

yes.you are right.the answare is 1


sorry answare is 0(wrongly written)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()))
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic