Programming/Arrays: Difference between revisions
Brodriguez (talk | contribs) (Expand page) |
Brodriguez (talk | contribs) m (Formatting) |
||
Line 14: | Line 14: | ||
{{ Tip | Some languages, such as JavaScript and Python are intelligent enough to handle array sizing for you, in the background. However, for most languages such as C, C#, and Java, you have to manage the size of the array yourself.}} | {{ Tip | Some languages, such as JavaScript and Python are intelligent enough to handle array sizing for you, in the background. However, for most languages such as C, C#, and Java, you have to manage the size of the array yourself.}} | ||
=== Resizing Arrays === | === Resizing Arrays === |
Revision as of 09:32, 7 May 2020
Arrays are one of the most common data structures.
For the most basic implementation (that most languages use), arrays are a sequential block of memory, where items are placed one after another from beginning to end. In other words, you can think of an array as a sequential, non-interrupting list of items.
Specifying an index declares which item number you wish to examine in the list.
Managing Arrays
Computers generally need to be explicitly told what to do. This is no exception for arrays. Thus, when an array is created, an explicit size needs to be provided so the computer understands how large to make it.
Furthermore, in most languages, each array can only hold one single data type. To be able to allocate multiple data types, either use multiple arrays or consider making an array of predefined Structs.
Resizing Arrays
Since array sizes need to be explicitly defined, they can't be arbitrarily expanded or shrunken. To change the size of an already existing array, create a new array of the desired size, then loop through the indexes of the old one and copy each value over.
For example, achieving this in C# may look as follows:
// Original array creation. Has 4 total elements, with indexes 0 through 3. old_array = new string[4] { "Apple", "Orange", "Pineapple", "Pear" }; ... // Updating array to hold another element. // Following the advice below, we upgrade our array to a size of 8. new_array = new string[8]; for (int index = 0; index < old_array.Length; index++) { new_array[index] = old_array[index]; }
Furthermore, consider keeping the array size as a power of 2. This is for two reasons:
- Due to how computer memory management works, powers of 2 tend to be the most efficient, space-wise.
- This forces creation of multiple new index values, instead of just one. That way, the array doesn't need to be recreated for each individual new index added. Instead it's recreated once in a while, and the extra indexes effectively provide a buffer for future additions.
For example, if you need an array of 5 elements, use a size of 8 (aka ).
Then, at a later date, say you hypothetically need to increase the size of this array to be four times larger (20 elements). Create a new array of size 32 (aka ), and then copy those initial 5 elements over from the original array.