views:

326

answers:

5

Yeah, it's a homework question, so givemetehkodezplsthx! :)

Anyway, here's what I need to do:
I need to have a class which will have among its attributes array of objects of another class. The proper way to do this in my opinion would be to use something like LinkedList, Vector or similar. Unfortunately, last time I did that, I got fire and brimstone from my professor, because according to his belief I was using advanced stuff without understanding basics.

Now next obvious solution would be to create array with fixed number of elements and add checks to get and set which will see if the array is full. If it is full, they'd create new bigger array, copy older array's data to the new array and return the new array to the caller. If it's mostly empty, they'd create new smaller array and move data from old array to new. To me this looks a bit stupid. For my homework, there probably won't be more that 3 elements in an array, but I'd like to make a scalable solution without manually calculating statistics about how often is array filled, what is the average number of new elements added, then using results of calculation to calculate number of elements in new array and so on.

By the way, there is no need to remove elements from the middle of the array.

Any tips?

+2  A: 

To copy an existing array into a smaller or larger one, you may find System#arrayCopy() useful.

Kickoff example:

Object[] originalArray = new Object[3];
// ...
Object[] resizedArray = new Object[originalArray.length + 2]; // Grow with 2.
System.arrayCopy(originalArray, 0, resizedArray, 0, originalArray.length);

This will copy the items over the entire length of originalArray into the beginning of resizedArray. The 2 slots at end of resizedArray are still null so that you can use it for other items.

This must get you started. Good luck :)

BalusC
Or you could write oldArray = Arrays.copyOf(oldArray, oldArray.length * 2 + 1); which does the same.
Helper Method
+3  A: 

One thing you will want to do is when you need to grow the size of your array create an array that is twice the size of the old array. Likewise, if you need to shrink the size of hte array, only do it once the array is half full.

This will make it so you have to do far less array copies.

Doing this will make it necessary to keep a variable that keeps track of the actual size of the array because the length of the array will not accurately represent the actual size.

jjnguy
Good idea, since I don't expect enough changes of the array length to warrant more complicated calculations.
AndrejaKo
"2" is just a common factor, but any multiple of the current size has the same effect statistically.
Joachim Sauer
+1  A: 

Is it for a data structures class ? Sounds like your professor expects you to implement your own Linked List data structure or something similar instead of using the one Java provides. Google and your text book(s) are your friend.

sjobe
No, it's not data structures class, it's Java and C# class. Actually, how would you make a traditional linked list without pointers? Have some sort of wrapper class which would have 2 references to objects of the class and a reference to data class and some other class which would link the wrapper class?
AndrejaKo
Use references and let garbage collection worry about cleaning up. Example here http://www.java-tips.org/java-se-tips/java.lang/linked-list-implementation-in-java.html
sjobe
+1  A: 

If I recall correctly, the ArrayList class works by having a fixed size array (with an initial capacity of whatever you set) that resizes in pretty much the way you described when it's full.

You could use a linked list, though by the sounds of it your professor wants you to program this stuff by yourself, so create your own class demonstrating you know how it works?

Moonshield
Actually, the point of the homework I'm doing at the moment is AWT. One of the requirement is to have a class which would extend Canvass and contain changeable number of objects of another class which extends Frame (if I understood the setup correctly).
AndrejaKo
+1  A: 
class test {
    private Object[] objects;
    private int size;

    public test() {
        objects = new Object[10];
        size = 0;
    }

    public void push(Object o) {
        if (objects.length == size) {
            throw new RuntimeException("This wouldn't happen if I didn't have to reinvent the wheel");
        }
        objects[size] = o;
        size++;
    }

    public Object pop() {
        size--;
        Object o = objects[size];
        objects[size] = null;
        return o;
    }
}

Just kidding. I think you're best bet is to implement your own linked list and then use that in your class. Something like:

class Element {
    Object val;
    Element next;
    Element prev;

    public Element(Object val, Element next, Element prev) {
        this.val = val;
        this.next = next;
        this.prev = prev;
    }

}

class LinkedList {
    Element head;
    Element tail;

    public void add(Object o) {
        Element el = new Element(o, null, tail);
        tail.next = el;
    }

    public Object remove() {
        Element o = tail;
        tail = o.prev;
        tail.next = null;
        return o.val;
    }
}
Andrei Fierbinteanu
Thank you for typing such an entertaining post. It sure made my day better! I'll think some more on this, but I'll probably use something similar to this.
AndrejaKo