Jump to content

Associative array

From Simple English Wikipedia, the free encyclopedia
Revision as of 14:56, 25 June 2025 by Wukuendo (talk | changes) (Uses: Wiki-link words)

An associative array is a special kind of array. Each element has a name or label instead of just a number. Usually, arrays use numbers to find things. With an associative array, the name or label is related to the element, as key to value (key-value or name-value). The key represents a name or label, that can be used to find the value of an element.

Example

  • The associative array is assigned to a variable (var).
  • The "key" is used to find the "value" it contains.
var := {
    "key_1": "value_1",
    "key_2": "value_2",
    "key_3": "value_3"
}

Uses

Associative arrays, depending on the programming language, can be known by many different names. Such as: hashmap, hash table, hash, map, table, or dictionary.

In Objective-C, .NET,[1] Python, Swift, VBA they are called dictionaries; in Perl, Ruby they are called hashes; in C++, C#, Go, Haskell, Java, OCaml, Scala, and V (Vlang)[2] they are called, maps; in Lisp (Common Lisp) and Windows PowerShell, they are called hash tables; in Maple and Lua, they are called tables. In the D language they are called hash maps.[3]

In JavaScript (see also JSON), all objects behave as associative arrays with string-value keys, while the Map and WeakMap types take objects as keys. In Lua, they are used as the building blocks for their data structures. In PHP and R, all arrays can be associative, except that the keys are limited to integers and strings.

References

  1. "Dictionary<TKey, TValue> Class". MSDN.
  2. Rao, Navule Pavan Kumar (December 10, 2021). Getting Started with V Programming. p. 103. ASIN B09FKK3JL7. ISBN 978-1839213434. OCLC 1290492862.
  3. "Associative Arrays, the D programming language". Digital Mars.