?
Solved

how to implement an expandable array?

Posted on 2004-04-10
8
Medium Priority
?
1,491 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 128 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 124 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
Industry Leaders: 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!

 

Assisted Solution

by:kimboon
kimboon earned 124 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 124 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

Independent Software Vendors: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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