Solved

how to implement an expandable array?

Posted on 2004-04-10
8
1,489 Views
Last Modified: 2013-11-23
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
Comment
Question by:rdong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 32 total points
ID: 10796818
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
 

Author Comment

by:rdong
ID: 10797883
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
 
LVL 3

Assisted Solution

by:shaggyb
shaggyb earned 31 total points
ID: 10798030
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Assisted Solution

by:kimboon
kimboon earned 31 total points
ID: 10798243
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
 
LVL 9

Assisted Solution

by:keteracel
keteracel earned 31 total points
ID: 10800708
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
 

Expert Comment

by:kimboon
ID: 10800918
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question