Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement expandable array

 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kamil Khan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mogens Nidding
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic