Solved

how to implement an expandable array?

Posted on 2004-04-10
8
1,482 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 49
hibernate jars 4 32
Facing this issue for maven proxy setting 2 13
CSV file parsing thru Java 13 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

822 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