aspose file tools*
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
Author

How to implement expandable array

Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
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.
Thanks,
Richard
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that the Java API source code is likely in a file named src.jar or src.zip 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
Greenhorn

Joined: Apr 25, 2003
Posts: 2
Hi,
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.
khan
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to implement expandable array