• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement expandable array

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic