Array: Difference between revisions

Content deleted Content added
m lang link fixing
Line 5:
:The implementation of '''arrays''' is based on setting the bounds of indices of the array, the ''size'' of the array, normally by allocating a contiguous region of memory to hold the elements of the array, and using simple offset calculations on the indices from the origin of the memory to access memory elements. Some languages support extensions to allow such arrays to be resized, or re-shaped, in which the memory area is adjusted, but extent elements are retained.
 
:By contrast an '''[[associative array]]''' maps the association between index "keys" and their associated values, generally using more complex [http://en.wikipedia.org/wiki/Hash_function hash functions] on the keys of the array to map them to their corresponding elements (by pointers, references or memory addresses of some sort). Associative arrays are referred to variously as "hashes" ([[Perl]]), "maps" or "mappings" ([[Lua]]), "dictionaries" ([[Python]]) as well as "associative arrays" ([[AwkAWK]], [[ksh]] and others). The keys into associative arrays are normally not constrained to be integers (unlike arrays which generally required contiguous integer ranges). Different languages may impose various constraints on these keys. For example in [[Perl]] keys are evaluated as Perl "scalar" values such that keys of 1 (a literal integer), "1" (a string representing the same), 1.0 (a literal floating point which is numerically equivalent) and "1.0" (another string representing a numerically equivalent value) will all map to the same key/value in a hash. Other languages (such as Python) may treat each type of object as distinct. (See [[associative array]] for further discussion).
 
:Non-associative arrays may have speed and memory consumption advantages. Associative arrays have greater flexibility in types used for keys and generally obviate the need to implement searches through the collection (Each component on which one would search can be implemented as a different associative array of references to their corresponding values or records).
Line 11:
Arrays with more than one index are called '''multidimensional''' arrays. For example, a matrix is a two-dimensional array.
 
Some languages (such as [[awkAWK]]) do not support true arrays and merely emulate them through their associative arrays. Similarly some languages emulate multi-dimensional arrays by concatenation of dimensional indices into keys (perhaps a peculiarity of [[awkAWK]]).
 
Common operations defined on arrays include: