Solved

how to implement an expandable array?

Posted on 2004-04-10
8
1,480 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
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now