Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Array (data type)

From Wikipedia, the free encyclopedia
(Redirected fromMulti-dimensional array)
Data type that represents an ordered collection of elements (values or variables)
This article is about the abstract data type. For the byte-level structure, seeArray (data structure). For other types of arrays, seeArray.

Incomputer science,array is adata type that represents a collection ofelements (values orvariables), each selected by one or more indices (identifying keys) that can be computed atrun time during program execution. Such a collection is usually called anarray variable orarray value.[1] By analogy with the mathematical conceptsvector andmatrix, array types with one and two indices are often calledvector type andmatrix type, respectively. More generally, a multidimensional array type can be called atensor type, by analogy with the mathematical concept,tensor.[2]

Language support for array types may include certainbuilt-in array data types, some syntactic constructions (array type constructors) that theprogrammer may use to define such types and declare array variables, and special notation for indexing array elements.[1] For example, in thePascal programming language, the declarationtypeMyTable=array[1..4,1..2]ofinteger, defines a new array data type calledMyTable. The declarationvar A: MyTable then defines a variableA of that type, which is an aggregate of eight elements, each being an integer variable identified by two indices. In the Pascal program, those elements are denotedA[1,1],A[1,2],A[2,1], …,A[4,2].[3] Special array types are often defined by the language's standardlibraries.

Dynamic lists are also more common and easier to implement[dubiousdiscuss] thandynamic arrays. Array types are distinguished fromrecord types mainly because they allow the element indices to be computed atrun time, as in the PascalassignmentA[I,J] := A[N-I,2*J]. Among other things, this feature allows a single iterativestatement to process arbitrarily many elements of an array variable.

In more theoretical contexts, especially intype theory and in the description of abstractalgorithms, the terms "array" and "array type" sometimes refer to anabstract data type (ADT) also calledabstract array or may refer to anassociative array, amathematical model with the basic operations and behavior of a typical array type in most languages – basically, a collection of elements that are selected by indices computed at run-time.

Depending on the language, array types may overlap (or be identified with) other data types that describe aggregates of values, such aslists andstrings. Array types are often implemented byarray data structures, but sometimes by other means, such ashash tables,linked lists, orsearch trees.

History

[edit]

Heinz Rutishauser's programming language Superplan (1949–1951) included multi-dimensional arrays. However, although Rutishauser described how a compiler for his language should be built, did not implement one.

Assembly languages and low-level languages like BCPL[4] generally have no syntactic support for arrays.

Because of the importance of array structures for efficient computation, the earliest high-level programming languages, includingFORTRAN (1957),COBOL (1960), andAlgol 60 (1960), provided support for multi-dimensional arrays.

Abstract arrays

[edit]

An array data structure can be mathematically modeled as anabstract data structure (anabstract array) with two operations

get(A,I): the data stored in the element of the arrayA whose indices are the integertupleI.
set(A,I,V): the array that results by setting the value of that element toV.

These operations are required to satisfy theaxioms[5]

get(set(A,I,V),I) =V
get(set(A,I,V),J) =get(A,J) ifIJ

for any array stateA, any valueV, and any tuplesI,J for which the operations are defined.

The first axiom means that each element behaves like a variable. The second axiom means that elements with distinct indices behave asdisjoint variables, so that storing a value in one element does not affect the value of any other element.

These axioms do not place any constraints on the set of valid index tuplesI, therefore this abstract model can be used fortriangular matrices and other oddly-shaped arrays.

Implementations

[edit]

In order to effectively implement variables of such types asarray structures (with indexing done bypointer arithmetic), many languages restrict the indices tointeger data types[6][7] (or other types that can be interpreted as integers, such asbytes andenumerated types), and require that all elements have the same data type and storage size. Most of those languages also restrict each index to a finiteinterval of integers, that remains fixed throughout the lifetime of the array variable. In somecompiled languages, in fact, the index ranges may have to be known atcompile time.

On the other hand, some programming languages provide more liberal array types, that allow indexing by arbitrary values, such asfloating-point numbers,strings,objects,references, etc.. Such index values cannot be restricted to an interval, much less a fixed interval. So, these languages usually allow arbitrary new elements to be created at any time. This choice precludes the implementation of array types as array data structures. That is, those languages use array-like syntax to implement a more generalassociative array semantics, and must therefore be implemented by ahash table or some othersearch data structure.

Language support

[edit]
icon
This sectionneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources in this section. Unsourced material may be challenged and removed.(January 2026) (Learn how and when to remove this message)

Type

[edit]

Languages have differing ways of defining an array type. For example, in C, an array is actually a block of contiguous memory, which is essentially treated as apointer.[8] In such cases, array declarations can decay to pointers:

inta[10];// array 'a' of 10 intsint*p=a;// 'p' points to the first element of 'a'voidfoo(intarr[]){// the parameter 'arr' is an int[]// 'arr' is decayed to a pointer to its first element}

However, in other languages, such asJava, an array is an actual type. For any typeT, it has a corresponding array typeT[], which is anobject with alength field.[9]

int[]a=newint[5];// declares an array 'a' of 5 ints

Multi-dimensional arrays

[edit]

The number of indices needed to specify an element is called thedimension,dimensionality, orrank of the array type. (This nomenclature conflicts with the concept of dimension in linear algebra, which expresses theshape of a matrix. Thus, an array of numbers with 5 rows and 4 columns, hence 20 elements, is said to have dimension 2 in computing contexts, but represents a matrix that is said to be 4×5-dimensional. Also, the computer science meaning of "rank" conflicts with the notion oftensor rank, which is a generalization of the linear algebra concept ofrank of a matrix.)

A two-dimensional array stored as a one-dimensional array of one-dimensional arrays (rows)
A two-dimensional array stored as a one-dimensional array of one-dimensional arrays (rows)

Many languages support only one-dimensional arrays. In those languages, a multi-dimensional array is typically represented by anIliffe vector, a one-dimensional array ofreferences to arrays of one dimension less. A two-dimensional array, in particular, would be implemented as a vector of pointers to its rows.[10] Thus an element in rowi and columnj of an arrayA would be accessed by double indexing (A[i][j] in typical notation). This way of emulating multi-dimensional arrays allows the creation ofjagged arrays, where each row may have a different size – or, in general, where the valid range of each index depends on the values of all preceding indices.

This representation for multi-dimensional arrays is quite prevalent in C and C++ software. However, C and C++ will use a linear indexing formula for multi-dimensional arrays that are declared with compile time constant size, e.g. byinta[10][20] orinta[m][n], instead of the traditionalint**a.[11]

The C99 standard introduced Variable Length Array types that let define array types with dimensions computed in run time. The dynamic 4D array can be constructed using a pointer to 4d array, e.g.int(*arr)[t][u][v][w]=malloc(sizeof*arr);. The individual elements are accessed by first de-referencing an array pointer followed by indexing, e.g.(*arr)[i][j][k][l]. Alternatively, n-d arrays can be declared as pointers to its first element which is a (n-1) dimensional array, e.g.int(*arr)[u][v][w]=malloc(t*sizeof*arr); and accessed using more idiomatic syntax, e.g.arr[i][j][k][l].

Indexing notation

[edit]

Most programming languages that support arrays support thestore andselect operations, and have special syntax for indexing. Early languages used parentheses, e.g.A(i,j), as in FORTRAN; others choose square brackets, e.g.A[i,j] orA[i][j], as in Algol 60 and Pascal (to distinguish from the use of parentheses forfunction calls).

Index types

[edit]

Array data types are most often implemented as array structures: with the indices restricted to integer (or totally ordered) values, index ranges fixed at array creation time, and multilinear element addressing. This was the case in most"third generation" languages, and is still the case of mostsystems programming languages such asAda,C, andC++. In some languages, however, array data types have the semantics of associative arrays, with indices of arbitrary type and dynamic element creation. This is the case in somescripting languages such asAwk andLua, and of some array types provided by standardC++ libraries.

Bounds checking

[edit]
Main article:Bounds checking

Some languages (like Pascal and Modula) performbounds checking on every access, raising anexception or aborting the program when any index is out of its valid range. Compilers may allow these checks to be turned off to trade safety for speed. Other languages (like FORTRAN and C) trust the programmer and perform no checks. Good compilers may also analyze the program to determine the range of possible values that the index may have, and this analysis may lead tobounds-checking elimination.

Index origin

[edit]
Further information:Comparison of programming languages (array)

Some languages, such as C, provide onlyzero-based array types, for which the minimum valid value for any index is 0.[12] This choice is convenient for array implementation and address computations. With a language such as C, a pointer to the interior of any array can be defined that will symbolically act as a pseudo-array that accommodates negative indices. This works only because C does not check an index against bounds when used.

Other languages provide onlyone-based array types, where each index starts at 1; this is the traditional convention in mathematics for matrices and mathematicalsequences. A few languages, such as Pascal and Lua, supportn-based array types, whose minimum legal indices are chosen by the programmer. The relative merits of each choice have been the subject of heated debate. Zero-based indexing can avoidoff-by-one orfencepost errors.[13]

Highest index

[edit]

The relation between numbers appearing in an array declaration and the index of that array's last element also varies by language. In many languages (such as C), one should specify the number of elements contained in the array; whereas in others (such as Pascal andVisual Basic .NET) one should specify the numeric value of the index of the last element. This distinction is not present in languages where the indices start at 1, such asLua.

Array algebra

[edit]

Some programming languages supportarray programming, where operations and functions defined for certain data types are implicitly extended to arrays of elements of those types. Thus one can writeA+B to add corresponding elements of two arraysA andB. Usually these languages provide both theelement-by-element multiplication and the standardmatrix product oflinear algebra, and which of these is represented by the* operator varies by language.

Languages providing array programming capabilities have proliferated since the innovations in this area ofAPL. These are core capabilities ofdomain-specific languages such asGAUSS,IDL,Matlab, andMathematica. They are a core facility in newer languages, such asJulia and recent versions ofFortran. These capabilities are also provided via standard extension libraries for other general purpose programming languages (such as the widely usedNumPy library forPython).

String types and arrays

[edit]

Many languages provide a built-instring data type, with specialized notation ("string literals") to build values of that type. In some languages (such as C), a string is just an array of characters, or is handled in much the same way.[14] Other languages, likePascal, may provide vastly different operations for strings and arrays.

Array index range queries

[edit]

Some programming languages provide operations that return the size (number of elements) of a vector, or, more generally, range of each index of an array. InC and C++, arrays do not support thesize() function, so programmers often have to declare separate variable to hold the size, and pass it to procedures as a separate parameter.

Elements of a newly created array may have undefined values (as in C), or may be defined to have a specific "default" value such as 0 or anull pointer (as in Java).

InC++ astd::vector object supports thestore,select, andappend operations with the performance characteristics discussed above.[15] Vectors can be queried for their size and can be resized. Slower operations like inserting an element in the middle are also supported.

Slicing

[edit]

Anarray slicing operation takes a subset of the elements of an array-typed entity (value or variable) and then assembles them as another array-typed entity, possibly with other indices. If array types are implemented as array structures, many useful slicing operations (such as selecting a sub-array, swapping indices, or reversing the direction of the indices) can be performed very efficiently by manipulating thedope vector of the structure. The possible slicings depend on the implementation details: for example,Fortran allows slicing off one column of a matrix variable, but not a row, and treat it as a vector.

On the other hand, other slicing operations are possible when array types are implemented in other ways.

Resizing

[edit]

Some languages allowdynamic arrays (also called resizable, growable, or extensible): array variables whose index ranges may be expanded at any time after creation, without changing the values of its current elements.

For one-dimensional arrays, this facility may be provided as an operationappend(A,x) that increases the size of the arrayA by one and then sets the value of the last element tox. Other array types (such as Pascal strings) provide a concatenation operator, which can be used together with slicing to achieve that effect and more. In some languages, assigning a value to an element of an array automatically extends the array, if necessary, to include that element. In other array types, a slice can be replaced by an array of different size, with subsequent elements being renumbered accordingly – as in Python's list assignmentA[5:5] = [10,20,30], that inserts three new elements (10, 20, and 30) before element "A[5]". Resizable arrays are conceptually similar tolists, and the two concepts are synonymous in some languages.

An extensible array can be implemented as a fixed-size array, with a counter that records how many elements are actually in use. Theappend operation merely increments the counter; until the whole array is used, when theappend operation may be defined to fail. This is an implementation of adynamic array with a fixed capacity, as in thestring type of Pascal. Alternatively, theappend operation may re-allocate the underlying array with a larger size, and copy the old elements to the new area.

See also

[edit]

References

[edit]
  1. ^abRobert W. Sebesta (2001)Concepts of Programming Languages. Addison-Wesley. 4th edition (1998), 5th edition (2001),ISBN 9780201385960
  2. ^"Introduction to Tensors | TensorFlow Core".TensorFlow.
  3. ^K. Jensen and Niklaus Wirth,PASCAL User Manual and Report. Springer. Paperback edition (2007) 184 pages,ISBN 978-3540069508
  4. ^John Mitchell,Concepts of Programming Languages. Cambridge University Press.
  5. ^Lukham, Suzuki (1979), "Verification of array, record, and pointer operations in Pascal".ACM Transactions on Programming Languages and Systems1 (2), 226–244.
  6. ^Deitel, Harvey M.; Deitel, Paul J. (2005).C# for Programmers. Prentice Hall Professional. p. 303.ISBN 978-0-13-246591-5. Retrieved22 May 2024.
  7. ^Friesen, Jeff (5 March 2014).Learn Java for Android Development: Java 8 and Android 5 Edition. Apress. p. 56.ISBN 978-1-4302-6455-2. Retrieved22 May 2024.
  8. ^"Array declaration".cppreference.com. cppreference. Retrieved15 November 2025.
  9. ^"Arrays (The Java Tutorials)".docs.oracle.com. Oracle Corporation. Retrieved15 November 2025.
  10. ^Van der Linden, Peter (1994).Expert C Programming: Deep C Secrets. Englewood Cliffs, NJ: SunSoft Press.ISBN 978-0-13-177429-2.
  11. ^Brian W. Kernighan and Dennis M. Ritchie (1988),The C programming Language. Prentice-Hall, p. 81.
  12. ^Kernighan, Brian W.; Ritchie, Dennis M. (1988).The C programming language (2nd ed.). Englewood Cliffs, N.J: Prentice Hall. p. 24.ISBN 978-0-13-110370-2.
  13. ^Edsger W. Dijkstra, "Why numbering should start at zero"
  14. ^"Null-terminated byte strings".cppreference.com. cppreference.com. Retrieved15 November 2025.
  15. ^"std::vector".cppreference.com. cppreference.com. Retrieved15 November 2025.

External links

[edit]
Wikibooks has a book on the topic of:Data Structures/Arrays
Look uparray in Wiktionary, the free dictionary.
Uninterpreted
Numeric
Reference
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Array_(data_type)&oldid=1331016195#Multi-dimensional_arrays"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp