• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is default size of ArrayList,List,LinkedList?

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Please tell me

Thanks
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Please tell me

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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks in advanced!
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic