File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to implement expandable array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to implement expandable array" Watch "How to implement expandable array" New topic

How to implement expandable array

Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 108
Can anyone provide a solution to this. This is a evaluation question which I did not do well and would like to see other people's solution
Write an expandable array like java.util.vector.
The code should contain:
private Object[];
purblic void add(Object o);
public int size();
public Object get(int index);
public void remove(int index);
also indicate where your code could be modified to have better efficiency.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The JDK comes with the Java source code, so you might have a look at the actual source for java.util.Vector and java.util.ArrayList.

[Jess in Action][AskingGoodQuestions]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Note that the Java API source code is likely in a file named src.jar or in your J2SE SDK installation folder.
For a decent introduction to and as a resource for building a better understanding of the subject of how to implement not just an expandable array, but other data structures as well, let me suggest bookmarking Bruno Preiss' free on-line book, Data Structures and Algorithms with Object-Oriented Design Patterns in Java, which includes step-by-step explanations of the aforementioned topics.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Kamil Khan

Joined: Apr 25, 2003
Posts: 2
I wonder if arrays are expandable what is the need for Vector or ArrayList?
As per theory, For arrays memory is allocated when array is declared where as for vector or arraylist it is in run time.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Originally posted by Kamil Khan:
I wonder if arrays are expandable what is the need for Vector or ArrayList?
Arrays are not expandable. That's exactly why we have Vector and ArrayList -- to implement the expandability feature on top of arrays...

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Mogens Nidding
Ranch Hand

Joined: Mar 08, 2004
Posts: 77
In essence, a Vector or ArrayList is merely an encapsulation of a totally ordinary, fixed-size array and a bunch of convenience methods. If you call the add method on it, it simply stores the element you want to add in the underlying array. If there is not room in that fixed-size array, the add method will create a new and larger, fixed-size array, copy the contents of the smaller array into the larger one, and discard the smaller array for you. This makes the Vector/ArrayList work like a variable-sized array to the user.
I agree. Here's the link:
subject: How to implement expandable array
jQuery in Action, 3rd edition