411 REFERENCE · Wikibooks
Programming Fundamentals/Fixed and Dynamic Arrays
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Overview
A fixed array is an array for which the size or length is determined when the array is created and/or allocated.Wikipedia: Array data type
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.Wikipedia: Dynamic array
Discussion
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.
LanguageDefined ValuesFixed-Length with Undefined or Default ValuesC++int values[] = {0, 1, 2};int values[3];C#int[] values = {0, 1, 2};int[] values = = new int[3];Javaint[] values = {0, 1, 2};int[] values = = new int[3];JavaScriptvar values = [0, 1, 2];var values = new Array(3);Pythonvalues = [0, 1, 2]values = [None] * 3Swiftvar 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.
LanguageClassAddRemoveC++#include <list>std::listinserteraseC#System.Collections.Generic.ListAddRemoveJavajava.util.ArrayListaddremoveJavaScriptArraypush, splicepop, splicePythonListappendremoveSwiftArrayappendremove
Key Terms
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