• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is default size of ArrayList,List,LinkedList?  RSS feed

 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!