Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1501
  • Last Modified:

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
0
rdong
Asked:
rdong
4 Solutions
 
CEHJCommented:
Since this is classwork, we can only give general advice. This is the most important method you'll need:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#arraycopy(java.lang.Object, int, java.lang.Object, int, int)
0
 
rdongAuthor Commented:
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
0
 
shaggybCommented:
i would implement a linked list

a linked list is a data structure in which a seiries of node objects with data types of Node nextNode in them   and they point to the next node in the list.......

http://en.wikipedia.org/wiki/Linked_list


check that out for some decent ifno
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kimboonCommented:
Try this:

-------------------------------------------
public class MyArray {
  private int max = 10;
  private Object[] objs = new Object[max];
  private int increment = 10;
  private int size = 0;

  public void add( Object o ) {
    if (size == max ) {
      max = max + increment;
      Object[] temp = new Object[max];
      System.arraycopy( objs, 0, temp, 0, size );
      objs = temp;
    }

    objs[size++] = o;
  }

  public int size() {
    return size;
  }

  public void remove(int index) {
    System.arraycopy(objs, index+1, objs, index, size-index);
    objs[--size] = null;
  }

  public Object get( int index) {
    if (index>=size || index <0) throw new ArrayIndexOutOfBoundsException();
    return objs[index];
  }
}
-------------------------------------------
kimboon
0
 
keteracelCommented:
If it's a skill evaluation then the first thing you should do is question why they don't allow you to use java.util.Vector (or java.util.LinkedList or any of the other Collections) as this is one of the main points of using an Object Orientated language: reuse of classes. Also, the people at Sun have been developing this for years so their solution will be far better optimised than anything you could come up with in a short period of time.

If you HAVE to create your own the one above looks about right EXCEPT the size you should return is the number of items that have been added, not the physical size of the array as if you try to return an uninitialised cell you'll get an unwanted Exception. (i.e. java.util.Vector returns the number of items added not the physical size of the internal storage array). Also, wherever possible, you should try to implement applicable interfaces. In this case, implementing the Collection  interface would make the class very useful (and powerful).
0
 
kimboonCommented:
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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now