programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# what is default size of ArrayList,List,LinkedList?

Ranch Hand
Posts: 214
• Number of slices to send:
Optional 'thank-you' note:
Hi all Ranchers,

My question is that what is the default size of ArrayList, List, LinkedList?
and these questions are below:

Q: If a vector has 2 elements then tell the size and capacity of its?
Q: If 10 elements in a ArrayList tell the size and capacity of its and how to iterate all these elements?

I faced both questions in interview but did not find exact answer.

Thanks

Bartender
Posts: 4179
22
• Number of slices to send:
Optional 'thank-you' note:

Rd Dari wrote:Hi all Ranchers,

My question is that what is the default size of ArrayList, List, LinkedList?

What does the API say? Note that there is a distinction between size and capacity.

and these questions are below:

Q: If a vector has 2 elements then tell the size and capacity of its?
Q: If 10 elements in a ArrayList tell the size and capacity of its and how to iterate all these elements?

I faced both questions in interview but did not find exact answer.

Thanks

What were your answers to those questions? What does the API say? These sorts of questions are a good chance to look up some information in the API and learn to use the documentation you have available.

Rd Dari
Ranch Hand
Posts: 214
• Number of slices to send:
Optional 'thank-you' note:
Hi all,

my answer was for first if size is 2 in this case then capacity is also 2.
and the same for vector if size is 10 then capacity is also 10.

I think I was wrong so please clear my all doubts.

Ranch Hand
Posts: 679
• Number of slices to send:
Optional 'thank-you' note:

Steve Luke wrote:What does the API say? These sorts of questions are a good chance to look up some information in the API and learn to use the documentation you have available.

Rd Dari
Ranch Hand
Posts: 214
• Number of slices to send:
Optional 'thank-you' note:
Hi,

As I know the number of elements in a vector or ArrayList actually stored is called by size, and the maximum number of elements which can be stored at a given instance of time called by Capacity.

"But in these how we distinguish the capacity of these".

Please suggest me a good solution to resolved it.

Thanks!

Bartender
Posts: 4568
9
• Number of slices to send:
Optional 'thank-you' note:
Have you read the documentation that people keep referring you to? Look here: java.util.ArrayList#ArrayList(), and you'll see it tells you what the default initial capacity is. If you search for the other references to "capacity" on that page you'll find out a bit more. Notice that some things are implementation details that are not specified.

For an ArrayList or Vector, the capacity is the size of the underlying array. LinkedLists are implemented differently. If you read the documentation for that class you won't see any references to capacity. That's because the concept of capacity doesn't make sense in that case.