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
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.
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
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.