Link to home
Start Free TrialLog in
Avatar of rdong
rdong

asked on

how to implement an expandable array?

Can anyone provide a solution to this:

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,

RD
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rdong
rdong

ASKER

Thanks a lot,

It is not a classwork but a skill evaluation question which I did not do very well. So I would like to see different solutions. But thanks for your direction.

RD
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi keteracel, the code I posted does return the number of items, study the code carefully I'm sure you will get it. :)

I agree with you that we should always consider the use of existing API. I'm sure rdong is aware of Vector class (since he mentioned it himself), but I believe his purpose is to understand how we can implement a Vector class using the basic array... Or maybe is just to know how Java array can be "expandable" ? I don't know, leave it to rdong to answer :)

BTW, to make my code posted above complete, I forgot to include the checking of index in remove() method.

Thanks.