An array object has slots that hold a number of other Lisp objects, called the elements of the array. Any element of an array may be accessed in constant time. In contrast, the time to access an element of a list is proportional to the position of that element in the list.
Emacs defines four types of array, all one-dimensional:
strings (see String Type), vectors (see Vector Type), bool-vectors (see Bool-Vector Type), and
char-tables (see Char-Table Type). Vectors and char-tables
can hold elements of any type, but strings can only hold characters,
and bool-vectors can only hold t
and nil
.
All four kinds of array share these characteristics:
aref
and aset
, respectively (see Functions that Operate on Arrays).
When you create an array, other than a char-table, you must specify its length. You cannot specify the length of a char-table, because that is determined by the range of character codes.
In principle, if you want an array of text characters, you could use either a string or a vector. In practice, we always choose strings for such applications, for four reasons:
By contrast, for an array of keyboard input characters (such as a key sequence), a vector may be necessary, because many keyboard input characters are outside the range that will fit in a string. See Key Sequence Input.