This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Java in General and the fly likes Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interview Question" Watch "Interview Question" New topic

Interview Question

Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
How to create array that can store 'n' number of elements? (without specifying the size)

(We are able to store 'n' number of elements dynamically using java.util.LinkedList, ArrayList right? How are they implemented to adopt any number of elements dynamically?)

Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
I guess even the collection classes uses arrays in the background which is not transparent to users.
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
yeah, thats what I am talking about. If anybody having jdk1.3 or less version, please post here the source code for java.util.ArrayList (jdk1.3 version)..
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
Got it. The collection starts with a fixed capacity. If the the elements size exceed than the current capacity, they are moved to a higher larger object array. previous one destroyed (nullified).

Thanks guys!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Well, some collections do that. Some don't. I'd guess a LinkedList is not backed by an array at all.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello friends,

One has to specify the size for normal arrays. The Collection class ArrayList is a wrapper over Object []. With Decorator pattern it is extending the functionality of the general arrays.

If you observe the java source, the constructor for arraylist is ,

* Constructs an empty list with an initial capacity of ten.
public ArrayList() {


* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
private transient Object elementData[];

* The size of the ArrayList (the number of elements it contains).
* @serial
private int size;

So, I think you got very clear view.

Thank you.
I agree. Here's the link:
subject: Interview Question
It's not a secret anymore!