Author Topic: Need help w/ Java  (Read 734 times)

Hey guys, I'm partially really stupid if you can't remember, and I need help with figuring out what exactly my documentation provided for a code lab is specifying for me to do.  I figured some of you have some knowledge in coding and computer science and all that and I might as well ask here while I'm scratching my head trying to figure it out.

We're forming a primitive array that will store objects while keeping track of a class-level boolean that determines whether or not nulls are allowed in the array, and there are four constructors to implement.
Quote
Code: [Select]
DynArray ( boolean allowNulls )
Creates a DynArray object that may allow or disallow its elements to be null values, depending on the value provided for the allowNulls parameter.  The internal array created by this constructor is a small power of two that is provided by the implementor.

I'm confused about what it means in the bold.  The class implements RandomAccess and Iterable.  Where am I getting a small power of two from?  Am I just putting in whatever small power of two I feel like?  The default constructor of DynArray() also says "the internal array is a small power of two determined by the implementation."

I don't know Java, but I'm fairly certain what the bolded text is telling you to do is to to create an array with length equal to a "small" power of 2
So something like 2^4 or 16.

Probably wants you to do this because when you need to increase the size of the array for whatever reason, it is more optimized to just double the array length (when you're going to be adding a variable amount of things to the list)(in this case it would be 2^5) instead of just arraylength+1 every time you need to increase the size.

I don't know Java, but I'm fairly certain what the bolded text is telling you to do is to to create an array with length equal to a "small" power of 2
So something like 2^4 or 16.

Probably wants you to do this because when you need to increase the size of the array for whatever reason, it is more optimized to just double the array length (when you're going to be adding a variable amount of things to the list)(in this case it would be 2^5) instead of just arraylength+1 every time you need to increase the size.
There is an add(T element) method, but it's a single element only, why would it be more optimized to double the array rather than arraylength+1?  Or do you mean it'd just generally be more optimized to double the array length when adding multiple things if I were, say, adding an entire DynArray?

It's because of how memory allocation works. If you only have "static" arrays without a linked list or anything more fancy, to make a larger array you have allocate the new size of array, copy EVERYTHING over to the new array. This takes a lot more time (O(n)) than just doubling the array length (O(log(n))) if you're adding to the list at a constant rate.

But you can throw that out the window if you're using a different container method, but those use slightly more memory because something has the save the memory location of the array "chain".

It's because of how memory allocation works. If you only have "static" arrays without a linked list or anything more fancy, to make a larger array you have allocate the new size of array, copy EVERYTHING over to the new array. This takes a lot more time (O(n)) than just doing this O(log(n)) times if you're adding to the list at a constant rate.

But you can throw that out the window if you're using a different container method, but those use slightly more memory because something has the save the memory location of the array "chain".
Right, gotcha.  We just went over linked lists and we know about Lists/ArrayLists but for some reason we have to do a lab right now that deals with this kind of vector.  We start a project on linked lists though tomorrow.

In further methods the documentation specified the use of a fixed class property "quanta" that basically was the same "small power of two", so I made a private final int of 2^4 and decided to use that.  I have to run to work but I'll leave it open until I return tonight in case anyone else wants to throw their two cents in about this, and I'll close it when I get back if nothing continues here.  Thanks for your help ZSNO!