File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interview Question" Watch "Interview Question" New topic
Author

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?)


Manivannan
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() {
this(10);
}

And

/**
* 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: http://aspose.com/file-tools
 
subject: Interview Question