• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList constructor  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Hi everyone in the above code My doubt is

public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.


but if I invoke size then it gives 0..???
here I hav created arraylist with initial
size 2 but why it gives size 0???


2>
public ArrayList()
Constructs an empty list with an initial capacity of ten.

but here also if I invoke size then it gives 0 as output..



please tell me whats the real meaning of it???

thnks in advance

with regards
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is backed by array.
Capacity and size are different, capacity means total space while size means occupied space.
If there is insufficient capacity, a new array would be created to hold the values which is useful if you know the capacity to avoid creating new array.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!