Programming Fundamentals/Fixed and Dynamic Arrays

Overview

edit

A fixed array is an array for which the size or length is determined when the array is created and/or allocated.[1]

A dynamic array is a random access, a variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation.[2]

Discussion

edit

Static arrays have their size or length determined when the array is created and/or allocated. For this reason, they may also be referred to as fixed-length arrays or fixed arrays. Array values may be specified when the array is defined, or the array size may be defined without specifying array contents. Depending on the programming language, an uninitialized array may contain default values, or it may contain whatever values were left in memory from previous allocation.

Language Defined Values Fixed-Length with Undefined or Default Values
C++ int values[] = {0, 1, 2}; int values[3];
C# int[] values = {0, 1, 2}; int[] values = = new int[3];
Java int[] values = {0, 1, 2}; int[] values = = new int[3];
JavaScript var values = [0, 1, 2]; var values = new Array(3);
Python values = [0, 1, 2] values = [None] * 3
Swift var values:[Int] = [0, 1, 2] var values: [Int] = [Int](repeating: 0, count: 3)

Dynamic arrays allow elements to be added and removed at runtime. Most current programming languages include built-in or standard library functions for creating and managing dynamic arrays.

Language Class Add Remove
C++ #include <list>
std::list
insert erase
C# System.Collections.Generic.List Add Remove
Java java.util.ArrayList add remove
JavaScript Array push, splice pop, splice
Python List append remove
Swift Array append remove

Key Terms

edit
dynamic array
A data structure consisting of a collection of elements that allows individual elements to be added or removed.
fixed array
A data structure consisting of a collection of elements for which the size or length is determined when the data structure is defined or allocated.

References

edit