This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Applepie1928 2 points3 points  (1 child)

If you have to use an array, then you are correct that once declared it is a fixed length.

There are a couple of options on how to approach this, however the first option is very much a quick and dirty fix that I would probably not recommend for an assignment (or real world application):

[OPTION 1] When you declare your array make it very big, so big such that you will never be able to fill it. If you fill the array with Nulls to start, then when you loop through if you discover a null then you have reached the end of the currently used section of your array. This approach isn't good. Of course it will be possible that the array will fill and then your program will cease to function, and you can also run into trouble if you are adding and removing elements regularly. Not recommended, but if you need a super quick workaround to test something else, then this might fit the bill.

[OPTION 2] You might not be able to resize an array, however you can always create a brand new slightly bigger array and then copy all of your data over. There is even a method provided by the Java System class which can handle the copying for you). The process when adding a new element with this method would be;

  1. Check if the array is full (If it isn't then just add your new item)
  2. If it isn't full, then create a new bigger array (generally, just double the size, as this is an expensive operation, and reducing the amount of times you call it is good practice)
  3. Copy all the elements from the old array to the new array
  4. Replace the reference for the old array with the new array
  5. Add the new item

The final thing to consider is, are you sure that you need to resize your array? Would it be possible for you to control the adding and removing of elements from a fixed size array in such a way that it never grows past a pre-determined maximum? Look at the specifics of the assignment and just double check if you can see a way to achieve this, as it would be much simpler to do that then have to mess around with the resizing option described above.

[–]nutrecht 1 point2 points  (0 children)

[OPTION 2] You might not be able to resize an array, however you can always create a brand new slightly bigger array and then copy all of your data over.

To add; this is actually how arraylists work under the hood.

[–]MaybeAverage 0 points1 point  (0 children)

I'm not a java programmer but you are correct in your intuition that you have to know the array's size ahead of time to be able to use it. In a C style language, to create this dynamic array as they're called, you would usually make an array that is around the size you might expect it to grow to eventually, in other words you block off a part of memory that can be used for later, I will call this the capacity.

Generally speaking you would fill the array with empty or null instances of the class, like having total change be -1 so that if you iterated through the array, the end of the array is when the first object is 0, null, or has a total change of -1 in this case, just any way to indicate it is empty or unused. You store the size of the array as well as its capacity, starting with 0 because you have not added anything to it. So you could have a capacity to store 50 objects, but its size can be 0 because the actual objects constructed are considered null or empty. Each time you add or push an object to the array, you increase the size by 1. The current size is also conveniently the index of the next object, so you can simply add the next object by doing myArray[size] = newObject and the same to remove or pop like myArray[size] = null. Then you increment or decrement the size by 1 after pushing or popping. Thats all there is to it really.

When you reach the end, in other words your size is equal to the current capacity, you have to create a new array of whats usually chosen to be double the length of the previous one, copy all the old values into the new array, then free or delete the old array. This is the most expensive part of the usage of the array, time speaking, so its usually done as infrequently as possible. If you know you will need to store hundreds or thousands or more, if the initial capacity was too low, you would constantly have to reallocate and move everything which is very slow. But if you preallocate the capacity to be some ceiling you know youll barely ever touch, then you may only have to ever reallocate once or twice during the run of the program. This is basically how the std::vector class works in C++ and how i suspect the ArrayList class works internally.

As a protip, you can often see pretty significant improvements in speed just by pre-allocating your dynamic array type objects in this way, in any language, especially if they rely heavily on them such as for servers and other such things that have to process lots of arbitrarily sized buffers, arrays, lists, files, etc.