Nepedia
Back to Discover
Advanced Reader
💻

Array (data structure)

technology Maturity 11-13 Next article →

In computer science, an array is a fundamental data structure. It consists of a collection of elements, which can be values or variables. Every element in an array must have the same memory size and data type. This uniformity allows the array to be a mutable and linear collection. Arrays are essential because they allow computers to organize and access data very efficiently. Almost every computer program uses them to manage information.

1D array diagram
1D array diagram

The way an array works is based on mathematical precision. Each element is identified by at least one array index, also known as a subscript. To find an element, the computer uses a specific formula to compute its memory address. This calculation starts from the base address, which is the starting position of the array in memory. For a one-dimensional array, the computer uses the index to find the exact location. If you have an array of ten 32-bit integers starting at address 2000, the element at index i is found at address 2000 + (i × 4). This mathematical link between the index and the address makes arrays incredibly fast for computers to process.

Arrays can take several different shapes or dimensions. A one-dimensional array is a simple linear list of items. A two-dimensional array forms a grid with rows and columns, often called a matrix. You can even have three-dimensional arrays, which add depth to the grid.

2D array diagram
2D array diagram

The number of indices required to find a specific element is known as the dimension, dimensionality, or rank. For example, a two-dimensional array requires two indices, like a row and a column number. A three-dimensional array would require three indices to locate a single piece of data.

3D array diagram
3D array diagram

The history of arrays is tied to the birth of digital computing. Early computers used machine-language programming to manage arrays for math and data tables. In 1945, John von Neumann wrote the first array-sorting program, known as a merge sort. This happened while the first stored-program computers were being built. Over time, programming languages evolved to make arrays easier to use. FORTRAN, created in 1957, provided support for multi-dimensional arrays. C, released in 1972, also included this support. Later, C++ introduced class templates in 1983 to handle arrays with dimensions fixed at runtime.

Different programming languages use different rules for numbering their indices. This is known as the indexing system. In zero-based indexing, the first element is at index 0. This is a popular design choice used by influential languages like C, Java, and Lisp. It works well because the index acts as an offset from the starting position. Other systems use one-based indexing, where the first element is index 1. Some languages, like Fortran 90 or Pascal, even allow users to choose their own starting index.

Row and column major order
Row and column major order

Arrays are used for many complex tasks in modern technology. They are used to implement other data structures, including lists, strings, stacks, and queues. Large databases often use one-dimensional arrays to store records. In mathematics, arrays are used to represent vectors and matrices. They can even be used to control the flow of a program through control tables. This allows a program to follow different paths based on the values stored in the array. Because they are so space-efficient, they are often used to emulate dynamic memory allocation in a program.

To manage these complex structures, computers often use a "dope vector." This is a record that contains the array's descriptor. It packs together important details like the dimension, the base address, and the address increments. It may also include the size of each element and the allowed range for indices. By manipulating the dope vector, a computer can perform tasks like slicing a sub-array or reversing the order of elements very quickly. This makes the array a powerful and flexible tool for both programmers and the machines they use.

Array of array storage
Array of array storage
650 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (5)
File:1D array diagram.svg
1D array diagram.svg
File:2D array diagram.svg
2D array diagram.svg
File:3D array diagram.svg
3D array diagram.svg
File:Row_and_column_major_order.svg
Row_and_column_major_order.svg
File:Array of array storage.svg
Array of array storage.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.